jQuery的.data()缓存在jQuery mobile中的页面转换是否持久? [英] Does jQuery's .data() cache persist across page transitions in jQuery mobile?

查看:220
本文介绍了jQuery的.data()缓存在jQuery mobile中的页面转换是否持久?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是阅读jQuery .data() API参考。事先,我推测它可能通过为页面元素分配'data - *'属性来工作;实际上是 $('#foo')。attr('data-foobar','bar')的快捷方式。

I was just reading the jQuery .data() API reference. Beforehand, I speculated that it might work by assigning 'data-*' attributes to page elements; effectively a shortcut to $('#foo').attr('data-foobar', 'bar').

(1)想象它是这样工作的,虽然它似乎没有,你从一个jQuery Mobile页面切换到另一个,然后再回来,将'data - *'属性仍然附加到元素,或者jQM是否有效地从下载的HTML重新加载?

(1) Imagine it did work like this, though it seems it doesn't, and you switched from one jQuery Mobile page to another and then back again, would the 'data-*' attributes still be attached to the elements, or does jQM effectively reload from the downloaded HTML?

但是,页面上的第一个注释说明:

In the reference, however, the first comment on the page states that:


数据不存储在元素上。它实际上存储在 $。cache - Alex

(2)这是真的吗?

(2) Is this true?

(3)如果我点击第2页并询问 $('#bar')。data('foo'),其中 #bar 是第1页的div,会发生什么?

(3) Does this persist across jQM page transitions? If I click on page 2 and ask $('#bar').data('foo'), where #bar is a div on page 1, what will happen?

你会说最好使用 .data('*' *')而不是 .attr('data - *','*')一个HTML文件,在只有 localStorage 可用于额外的应用程序存储的情况下运行在Cordova / PhoneGap之上?

Would you say it was preferable to use .data('*','*') instead of .attr('data-*','*') in the context of a jQM app, with all content in one HTML file, running on top of Cordova/PhoneGap in a situation where only localStorage is available for extra-app storage?

具体,是的。稍微肛门,是的。

Specific, yes. Slightly anal, yes.

期待您的答案。是的。

推荐答案

如果您使用多页模板在单个文档中具有多个 data-role =page元素,则数据将通过转换持续存在。这是因为所有的页面总是在DOM中,并且它们永远不会从DOM中删除。

If your using the multi-page template where you have multiple data-role="page" elements in a single document, then the data will persists through transitions. This is because all of the pages are always in the DOM and they are never removed from the DOM.

现在,当你通过AJAX插入一个伪页面时,这是一个不同的故事。当链接到远程页面时,jQuery Mobile通过AJAX抓取页面,然后将其转换为视图。默认情况下,当您离开该页面时,它将从DOM中删除以节省内存。当页面被删除时,其数据也是如此。您可以通过在任何外部 data-role =page上设置 data-cache =true属性来停止此行为元素。

Now, when you pull-in a pseudo-page via AJAX, it's a different story. When you link to a remote page, jQuery Mobile grabs the page via AJAX and then transitions it into view. By default, when you leave that page it will be removed from the DOM to save memory. When the page is removed, so is its data. You can stop this behavior by setting the data-cache="true" attribute on any external data-role="page" elements.

文件: http://jquerymobile.com/demos/1.1.0/docs/pages/page-cache.html

这篇关于jQuery的.data()缓存在jQuery mobile中的页面转换是否持久?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆