简体   繁体   English

如何在不使用window对象的情况下在jquery移动页面之间共享json数据

[英]How to share json data between jquery mobile pages with out using window object

In index.html page on "pageinit" event, I am getting all the employees details from the back end. “ pageinit”事件的index.html页面中,我从后端获取所有员工详细信息。

Once I fetch the employees data, I am storing it in "employees" variable for future access. 提取员工数据后,我会将其存储在“员工”变量中以供将来访问。

The sample code looks like this: 示例代码如下所示:

var employees;
var url = "/employee-jquerymobile/services/";

$("#employeeListPage").bind('pageinit', function() {
    var method = url + "getemployees.php";
    $.getJSON(method,  function(data) {
        var i, emp;
        employees = data.items;     
        var $employeeListView = $("#employeeListView");
        $employeeListView.find("li").remove();      

        for (i=0; i<employees.length; i++) {
            emp = employees[i];         
            $employeeListView.append(
                "<li><a data-transition='slide' href='employeedetails.html?id=" +  emp.id +  "'>" + emp.firstName + "</a></li>"
            );      
        }

        $employeeListView.listview("refresh");

    });
});

I have one more view / html file called "employeedetails.html", where I want to show the respected user information. 我还有一个视图/ html文件,名为“ employeedetails.html”,在此我想显示受尊重的用户信息。

The sample code for employeedetails.html is as below: employeedetails.html的示例代码如下:

<div id="detailsPage" data-role="page" data-add-back-btn="true">

    <div data-role="header">
        <h1>Employee Details</h1>
    </div>

    <div data-role="content"> 
        <img id="employeePic"/>
        <div id="employeeDetails">
        <h3 id="fullName"></h3>
        <p id="employeeTitle"></p>
        <p id="city"></p>
    </div>

</div>

In "employeedetails.html", I am unable to access the previously stored "employees" variable. 在“ employeedetails.html”中,我无法访问以前存储的“ employees”变量。 I checked in console and it gives "undefined". 我检查了控制台,它给出了“未定义”。

I have searched the internet and found, few guyz suggested by using the employee id, I call the back end service again and fetch the employee data. 我在互联网上搜索后发现,很少有人通过使用员工ID提出建议,我再次致电后端服务并获取员工数据。

Update: I am able to access the variable, if I make it globlal using window object. 更新: 如果使用window对象将其设置为全局变量,则可以访问该变量。 But is it the recommended way to make the data public. 但这是将数据公开的推荐方法。 Is there any other way to share the data between pages or views in jQuery Mobile. 还有其他方法可以在jQuery Mobile中的页面或视图之间共享数据。 One last option is local storage / session storage, but are there any other options 最后一个选择是本地存储/会话存储,但是还有其他选择吗

I tried a lot but failed. 我尝试了很多但是失败了。 Any help would be greatly appreciated, Thanks in Advance. 任何帮助将不胜感激,在此先感谢。

Use sessionStorage: 使用sessionStorage:

...
$.getJSON(method,  function(data) {
    var i, emp;
    sessionStorage.employees = JSON.stringify(data.items);
...

Then you could get this data back using: 然后,您可以使用以下方式获取此数据:

var employees=JSON.parse(sessionStorage.employees);

EDIT: To check the availability of "sessionStorage" in the browser: 编辑:要在浏览器中检查“ sessionStorage”的可用性:

if (window.sessionStorage){
    ... //do whatever
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM