現在、私は私が作業している私のアプリケーションのためのこのコードを持っています。私はバックボーンに問題があります。私があなたを助けてくれることを願っています。Backbone.js。 JSONデータをローカルストレージに保存します
の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[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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データを持っていますが、私はこのデータを扱っています。しかし、今では、JSONデータをURLからローカルストレージに1日に1回だけ保存し、ローカルストレージに保存したいと考えています。
1日に1回新鮮なデータを取得してローカルストレージに保存し、ローカルストレージからこのデータを操作するので、私が行っているすべてのことからURLからJSONをリクエストする必要はありません。
(EmployeeModel.js)私はurlからコレクションにデータを取得し、それをlocalstorageに保存しますが、今は失われています。私はeasblyバックボーンで失われる。
ありがとうございました。
問題は何ですか? –
私はすでにローカルストレージにjsonを持っていますが、localstorage-adapter.jsで見ることができます。しかし、私はURLからそれを取得したい。私はEmployeeModel.jsで何かを始めました。今、私がアプリケーションを実行するとき、私はURLからデータを取得します。 しかし、問題は、URLからデータを1日に1回取得し、ローカルストレージに保存したいということです。そして、私はアプリケーションでデータを使用するときに、私はそれをローカルストレージからロードしたい。 – Chufta
チェックアウトhttps://github.com/jeromegn/Backbone.localStorage –