jQuery Mobile 页面刷新机制

发布于 2024-12-08 17:04:53 字数 460 浏览 1 评论 0原文

我很难理解 jQuery Mobile 如何在 ajax 更新后处理页面刷新。

我有一个两页的独特文件网站:一个搜索引擎。

第一页是搜索字段。提交会触发 JSON 调用和解析器,从而更新第二页:结果。

现在我正在使用: $.mobile.changePage( $('#result') ); 它可以很好地完成从搜索字段到结果页面的工作。 然而: 如果我在下一个/上一个页面的结果页面中重用它(新的 json 调用、新的解析、DOM 中新添加的节点); Jquery Mobile 只是不“绘制”新添加的节点。

谁能解释一下,请使用和区别 1- $.mobile.page() 2- $.mobile.changePage() 3- $.mobile.refresh()

或给我一个关于如何处理页面更改的提示。 谢谢!

I'm having a lot of pain understanding how jQuery Mobile handles pages refresh after an ajax update.

I'm having a two pages - unique file site: a search engine.

First page is a search field. Submit triggers a JSON call and parser which updates the second page: results.

for now i'm using: $.mobile.changePage( $('#result') ); which does the job great from search field to result page.
However:
If I reuse it from result page for next/prev pages ( new json call, new parse, new added nodes in the DOM );
Jquery Mobile just don't "paint" the newly added nodes.

can anyone explain, please the use and distinction of
1- $.mobile.page()
2- $.mobile.changePage()
3- $.mobile.refresh()

or give me a hint on how I should handle page changes.
thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

野鹿林 2024-12-15 17:04:53
function refreshPage()
{
    jQuery.mobile.changePage(window.location.href, {
        allowSamePageTransition: true,
        transition: 'none',
        reloadPage: true
    });
}

摘自这里 http ://scottwb.com/blog/2012/06/29/reload-the-same-page-without-blinking-on-jquery-mobile/ 也在 jQuery Mobile 上进行了测试1.2.0

function refreshPage()
{
    jQuery.mobile.changePage(window.location.href, {
        allowSamePageTransition: true,
        transition: 'none',
        reloadPage: true
    });
}

Taken from here http://scottwb.com/blog/2012/06/29/reload-the-same-page-without-blinking-on-jquery-mobile/ also tested on jQuery Mobile 1.2.0

薄荷→糖丶微凉 2024-12-15 17:04:53

请仔细看看这里: http://jquerymobile.com/test/docs/api /methods.html

$.mobile.changePage() 是从一个页面切换到另一个页面,参数可以是url,也可以是page对象。 (只有 #result 也可以)

不再推荐 $.mobile.page(),请使用 .trigger( "create"),另请参阅:JQuery Mobile .page() 函数导致无限循环?

重要提示:
创建与刷新:重要区别

请注意,某些小部件的创建事件和刷新方法之间存在重要区别。 create 事件适合增强包含一个或多个小部件的原始标记。某些小部件具有的刷新方法应该用于已通过编程方式操作并需要更新 UI 来匹配的现有(已增强)小部件。

例如,如果您有一个页面,在创建页面后使用 data-role=listview 属性动态附加新的无序列表,则在该列表的父元素上触发 create 会将其转换为 listview 样式的小部件。如果随后以编程方式添加更多列表项,则调用列表视图的刷新方法将仅将这些新列表项更新为增强状态,并保持现有列表项不变。

我猜 $.mobile.refresh() 不存在

那么您使用什么来获取结果?列表视图?然后您可以通过执行以下操作来更新它:

$('ul').listview('refresh');

示例:
http:// /operationmobile.com/dont-forget-to-call-refresh-when-adding-items-to-your-jquery-mobile-list/

否则你可以这样做:

$('#result').live("pageinit", function(){ // or pageshow
    // your dom manipulations here
});

Please take a good look here: http://jquerymobile.com/test/docs/api/methods.html

$.mobile.changePage() is to change from one page to another, and the parameter can be a url or a page object. ( only #result will also work )

$.mobile.page() isn't recommended anymore, please use .trigger( "create"), see also: JQuery Mobile .page() function causes infinite loop?

Important:
Create vs. refresh: An important distinction

Note that there is an important difference between the create event and refresh method that some widgets have. The create event is suited for enhancing raw markup that contains one or more widgets. The refresh method that some widgets have should be used on existing (already enhanced) widgets that have been manipulated programmatically and need the UI be updated to match.

For example, if you had a page where you dynamically appended a new unordered list with data-role=listview attribute after page creation, triggering create on a parent element of that list would transform it into a listview styled widget. If more list items were then programmatically added, calling the listview’s refresh method would update just those new list items to the enhanced state and leave the existing list items untouched.

$.mobile.refresh() doesn't exist i guess

So what are you using for your results? A listview? Then you can update it by doing:

$('ul').listview('refresh');

Example:
http://operationmobile.com/dont-forget-to-call-refresh-when-adding-items-to-your-jquery-mobile-list/

Otherwise you can do:

$('#result').live("pageinit", function(){ // or pageshow
    // your dom manipulations here
});
梅倚清风 2024-12-15 17:04:53

我在 jQuery 论坛上发布了这一点(我希望它能有所帮助):

深入研究 jQM 代码我找到了这个解决方案。我希望它可以帮助其他人:

刷新动态修改的页面:

function refreshPage(page){
    // Page refresh
    page.trigger('pagecreate');
    page.listview('refresh');
}

即使您创建新的页眉、导航栏或页脚,它也能正常工作。我已经用 jQM 1.0.1 对其进行了测试。

I posted that in jQuery forums (I hope it can help):

Diving into the jQM code i've found this solution. I hope it can help other people:

To refresh a dynamically modified page:

function refreshPage(page){
    // Page refresh
    page.trigger('pagecreate');
    page.listview('refresh');
}

It works even if you create new headers, navbars or footers. I've tested it with jQM 1.0.1.

-小熊_ 2024-12-15 17:04:53

我发现这个线程希望使用 jQuery Mobile 创建 ajax 页面刷新按钮。

@sgissinger 有最接近我正在寻找的答案,但它已经过时了。

我更新了 jQuery Mobile 1.4

function refreshPage() {
  jQuery.mobile.pageContainer.pagecontainer('change', window.location.href, {
    allowSamePageTransition: true,
    transition: 'none',
    reloadPage: true 
    // 'reload' parameter not working yet: //github.com/jquery/jquery-mobile/issues/7406
  });
}

// Run it with .on
$(document).on( "click", '#refresh', function() {
  refreshPage();
});

I found this thread looking to create an ajax page refresh button with jQuery Mobile.

@sgissinger had the closest answer to what I was looking for, but it was outdated.

I updated for jQuery Mobile 1.4

function refreshPage() {
  jQuery.mobile.pageContainer.pagecontainer('change', window.location.href, {
    allowSamePageTransition: true,
    transition: 'none',
    reloadPage: true 
    // 'reload' parameter not working yet: //github.com/jquery/jquery-mobile/issues/7406
  });
}

// Run it with .on
$(document).on( "click", '#refresh', function() {
  refreshPage();
});
灯角 2024-12-15 17:04:53

我通过在我想要刷新的页面上的页面 div 中使用 data-cache="false" 属性解决了这个问题。

<div data-role="page" data-cache="false">
/*content goes here*/

</div>

就我而言,这是我的购物车。如果客户将一件商品添加到购物车,然后继续购物,然后将另一件商品添加到购物车,则购物车页面将不会显示新商品。除非他们刷新页面。据我所知,将 data-cache 设置为 false 指示 JQM 不要缓存该页面。

希望这对将来的其他人有帮助。

I solved this problem by using the the data-cache="false" attribute in the page div on the pages I wanted refreshed.

<div data-role="page" data-cache="false">
/*content goes here*/

</div>

In my case it was my shopping cart. If a customer added an item to their cart and then continued shopping and then added another item to their cart the cart page would not show the new item. Unless they refreshed the page. Setting data-cache to false instructs JQM not to cache that page as far as I understand.

Hope this helps others in the future.

涙—继续流 2024-12-15 17:04:53

这个答案对我有用 http:// view.jquerymobile.com/master/demos/faq/injected-content-is-not-enhanced.php

在多页面模板的上下文中,我修改了 Javascript 'pagebeforeshow' 处理程序和触发器中

...

的内容脚本末尾刷新:

$(document).bind("pagebeforeshow", function(event,pdata) {
  var parsedUrl = $.mobile.path.parseUrl( location.href );
  switch ( parsedUrl.hash ) {
    case "#p_02":
      ... some modifications of the content of the <div> here ...
      $("#foo").trigger("create");
    break;
  }
});

This answer did the trick for me http://view.jquerymobile.com/master/demos/faq/injected-content-is-not-enhanced.php.

In the context of a multi-pages template, I modify the content of a <div id="foo">...</div> in a Javascript 'pagebeforeshow' handler and trigger a refresh at the end of the script:

$(document).bind("pagebeforeshow", function(event,pdata) {
  var parsedUrl = $.mobile.path.parseUrl( location.href );
  switch ( parsedUrl.hash ) {
    case "#p_02":
      ... some modifications of the content of the <div> here ...
      $("#foo").trigger("create");
    break;
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文