目前,我正在使用我的应用程序的代码。我对骨干有困难,希望你能帮助我。
的localStorage-adapter.js
app.adapters.employee = (function () {
console.log("Loading localstorage adapter module");
var findById = function (id) {
var deferred = $.Deferred(),
employees = JSON.parse(window.localStorage.getItem("employees")),
employee = null,
l = employees.length;
for (var i = 0; i < l; i++) {
if (employees[i].id === id) {
employee = employees[i];
break;
}
}
deferred.resolve(employee);
return deferred.promise();
},
findByName = function (searchKey) {
var deferred = $.Deferred(),
employees = JSON.parse(window.localStorage.getItem("employees")),
results = employees.filter(function (element) {
var fullName = element.firstName + " " + element.lastName;
return fullName.toLowerCase().indexOf(searchKey.toLowerCase()) > -1;
});
deferred.resolve(results);
return deferred.promise();
},
findByManager = function (managerId) {
var deferred = $.Deferred(),
employees = JSON.parse(window.localStorage.getItem("employees")),
results = employees.filter(function (element) {
return managerId === element.managerId;
});
deferred.resolve(results);
return deferred.promise();
};
// Store sample data in Local Storage
window.localStorage.setItem("employees", JSON.stringify(
[
{"id": 1, "firstName": "James", "lastName": "King", "managerId": 0, "managerName": "", "reports": 4, "title": "President and CEO", "department": "Corporate", "cellPhone": "617-000-0001", "officePhone": "781-000-0001", "email": "jking@fakemail.com", "city": "Boston, MA", "pic": "james_king.jpg", "twitterId": "@fakejking", "blog": "http://coenraets.org"},
{"id": 2, "firstName": "Julie", "lastName": "Taylor", "managerId": 1, "managerName": "James King", "reports": 2, "title": "VP of Marketing", "department": "Marketing", "cellPhone": "617-000-0002", "officePhone": "781-000-0002", "email": "jtaylor@fakemail.com", "city": "Boston, MA", "pic": "julie_taylor.jpg", "twitterId": "@fakejtaylor", "blog": "http://coenraets.org"},
{"id": 3, "firstName": "Eugene", "lastName": "Lee", "managerId": 1, "managerName": "James King", "reports": 0, "title": "CFO", "department": "Accounting", "cellPhone": "617-000-0003", "officePhone": "781-000-0003", "email": "elee@fakemail.com", "city": "Boston, MA", "pic": "eugene_lee.jpg", "twitterId": "@fakeelee", "blog": "http://coenraets.org"},
{"id": 4, "firstName": "John", "lastName": "Williams", "managerId": 1, "managerName": "James King", "reports": 3, "title": "VP of Engineering", "department": "Engineering", "cellPhone": "617-000-0004", "officePhone": "781-000-0004", "email": "jwilliams@fakemail.com", "city": "Boston, MA", "pic": "john_williams.jpg", "twitterId": "@fakejwilliams", "blog": "http://coenraets.org"},
{"id": 5, "firstName": "Ray", "lastName": "Moore", "managerId": 1, "managerName": "James King", "reports": 2, "title": "VP of Sales", "department": "Sales", "cellPhone": "617-000-0005", "officePhone": "781-000-0005", "email": "rmoore@fakemail.com", "city": "Boston, MA", "pic": "ray_moore.jpg", "twitterId": "@fakermoore", "blog": "http://coenraets.org"},
{"id": 6, "firstName": "Paul", "lastName": "Jones", "managerId": 4, "managerName": "John Williams", "reports": 0, "title": "QA Manager", "department": "Engineering", "cellPhone": "617-000-0006", "officePhone": "781-000-0006", "email": "pjones@fakemail.com", "city": "Boston, MA", "pic": "paul_jones.jpg", "twitterId": "@fakepjones", "blog": "http://coenraets.org"},
{"id": 7, "firstName": "Paula", "lastName": "Gates", "managerId": 4, "managerName": "John Williams", "reports": 0, "title": "Software Architect", "department": "Engineering", "cellPhone": "617-000-0007", "officePhone": "781-000-0007", "email": "pgates@fakemail.com", "city": "Boston, MA", "pic": "paula_gates.jpg", "twitterId": "@fakepgates", "blog": "http://coenraets.org"},
{"id": 8, "firstName": "Lisa", "lastName": "Wong", "managerId": 2, "managerName": "Julie Taylor", "reports": 0, "title": "Marketing Manager", "department": "Marketing", "cellPhone": "617-000-0008", "officePhone": "781-000-0008", "email": "lwong@fakemail.com", "city": "Boston, MA", "pic": "lisa_wong.jpg", "twitterId": "@fakelwong", "blog": "http://coenraets.org"},
{"id": 9, "firstName": "Gary", "lastName": "Donovan", "managerId": 2, "managerName": "Julie Taylor", "reports": 0, "title": "Marketing Manager", "department": "Marketing", "cellPhone": "617-000-0009", "officePhone": "781-000-0009", "email": "gdonovan@fakemail.com", "city": "Boston, MA", "pic": "gary_donovan.jpg", "twitterId": "@fakegdonovan", "blog": "http://coenraets.org"},
{"id": 10, "firstName": "Kathleen", "lastName": "Byrne", "managerId": 5, "managerName": "Ray Moore", "reports": 0, "title": "Sales Representative", "department": "Sales", "cellPhone": "617-000-0010", "officePhone": "781-000-0010", "email": "kbyrne@fakemail.com", "city": "Boston, MA", "pic": "kathleen_byrne.jpg", "twitterId": "@fakekbyrne", "blog": "http://coenraets.org"},
{"id": 11, "firstName": "Amy", "lastName": "Jones", "managerId": 5, "managerName": "Ray Moore", "reports": 0, "title": "Sales Representative", "department": "Sales", "cellPhone": "617-000-0011", "officePhone": "781-000-0011", "email": "ajones@fakemail.com", "city": "Boston, MA", "pic": "amy_jones.jpg", "twitterId": "@fakeajones", "blog": "http://coenraets.org"},
{"id": 12, "firstName": "Steven", "lastName": "Wells", "managerId": 4, "managerName": "John Williams", "reports": 0, "title": "Software Architect", "department": "Engineering", "cellPhone": "617-000-0012", "officePhone": "781-000-0012", "email": "swells@fakemail.com", "city": "Boston, MA", "pic": "steven_wells.jpg", "twitterId": "@fakeswells", "blog": "http://coenraets.org"}
]
));
// The public API
return {
findById: findById,
findByName: findByName,
findByManager: findByManager
};
}());
EmployeeModel.js
app.models.Employee = Backbone.Model.extend({
});
app.models.EmployeeCollection = Backbone.Collection.extend({
model: app.models.Employee,
url: 'http://www.bonar.si/api/restaurants',
initialize: function(){
console.log("Employees initialize");
},
parse : function(response) {
console.log("response", response);
localStorage.setItem('json', JSON.stringify(response));
console.log("local storage:", localStorage.getItem('json'));
return response;
} });
所以,我在我的本地存储中有JSON数据,然后我正在使用这些数据。但现在我想每天只将URL中的JSON数据保存到本地存储一次,并将其保存到本地存储。
所以想法是每天获取一次新数据并将其保存到本地存储,然后使用本地存储中的这些数据进行操作,因此我不需要从URL请求JSON。
在EmployeeModel.js中的我从url获取数据到集合并将其保存到localstorage,但现在我迷路了。我很容易迷失骨干。
感谢所有的帮助。
答案 0 :(得分:0)
因此,如果我理解正确,您需要每天刷新一次员工数据吗? 我的一个应用就是这样做的。我的方法是使用刷新时间戳保留localStorage变量。 每次打开应用程序时都会调用刷新函数(来自onDeviceReady函数)。如果localStorage中的刷新时间戳早于一天,或者为null,则会继续从URL获取数据,并将更新日期设置为现在。