Download this file

localstorage_adapter.js    129 lines (111 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*global Ember*/
/*global DS*/
'use strict';
DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
init: function () {
this._loadData();
},
generateIdForRecord: function () {
return Math.random().toString(32).slice(2).substr(0, 5);
},
find: function (store, type, id) {
var namespace = this._namespaceForType(type);
return Ember.RSVP.resolve(Ember.copy(namespace.records[id]));
},
findMany: function (store, type, ids) {
var namespace = this._namespaceForType(type);
var results = [];
for (var i = 0; i < ids.length; i++) {
results.push(Ember.copy(namespace.records[ids[i]]));
}
return Ember.RSVP.resolve(results);
},
// Supports queries that look like this:
//
// {
// <property to query>: <value or regex (for strings) to match>,
// ...
// }
//
// Every property added to the query is an "AND" query, not "OR"
//
// Example:
//
// match records with "complete: true" and the name "foo" or "bar"
//
// { complete: true, name: /foo|bar/ }
findQuery: function (store, type, query, recordArray) {
var namespace = this._namespaceForType(type);
var results = this.query(namespace.records, query);
return Ember.RSVP.resolve(results);
},
query: function (records, query) {
var results = [];
var id, record, property, test, push;
for (id in records) {
record = records[id];
for (property in query) {
test = query[property];
push = false;
if (Object.prototype.toString.call(test) === '[object RegExp]') {
push = test.test(record[property]);
} else {
push = record[property] === test;
}
}
if (push) {
results.push(record);
}
}
return results;
},
findAll: function (store, type) {
var namespace = this._namespaceForType(type);
var results = [];
for (var id in namespace.records) {
results.push(Ember.copy(namespace.records[id]));
}
return Ember.RSVP.resolve(results);
},
createRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
this._addRecordToNamespace(namespace, record);
this._saveData();
return Ember.RSVP.resolve();
},
updateRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
var id = record.get('id');
namespace.records[id] = record.toJSON({ includeId: true });
this._saveData();
return Ember.RSVP.resolve();
},
deleteRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
var id = record.get('id');
delete namespace.records[id];
this._saveData();
return Ember.RSVP.resolve();
},
// private
_getNamespace: function () {
return this.namespace || 'DS.LSAdapter';
},
_loadData: function () {
var storage = localStorage.getItem(this._getNamespace());
this._data = storage ? JSON.parse(storage) : {};
},
_saveData: function () {
localStorage.setItem(this._getNamespace(), JSON.stringify(this._data));
},
_namespaceForType: function (type) {
var namespace = type.url || type.toString();
return this._data[namespace] || (
this._data[namespace] = {records: {}}
);
},
_addRecordToNamespace: function (namespace, record) {
var data = record.serialize({includeId: true});
namespace.records[data.id] = data;
}
});