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