| 1 | _kiwi.model.DataStore = Backbone.Model.extend({ |
| 2 | initialize: function () { |
| 3 | this._namespace = ''; |
| 4 | this.new_data = {}; |
| 5 | }, |
| 6 | |
| 7 | namespace: function (new_namespace) { |
| 8 | if (new_namespace) this._namespace = new_namespace; |
| 9 | return this._namespace; |
| 10 | }, |
| 11 | |
| 12 | // Overload the original save() method |
| 13 | save: function () { |
| 14 | localStorage.setItem(this._namespace, JSON.stringify(this.attributes)); |
| 15 | }, |
| 16 | |
| 17 | // Overload the original load() method |
| 18 | load: function () { |
| 19 | if (!localStorage) return; |
| 20 | |
| 21 | var data; |
| 22 | |
| 23 | try { |
| 24 | data = JSON.parse(localStorage.getItem(this._namespace)) || {}; |
| 25 | } catch (error) { |
| 26 | data = {}; |
| 27 | } |
| 28 | |
| 29 | this.attributes = data; |
| 30 | } |
| 31 | }, |
| 32 | |
| 33 | { |
| 34 | // Generates a new instance of DataStore with a set namespace |
| 35 | instance: function (namespace, attributes) { |
| 36 | var datastore = new _kiwi.model.DataStore(attributes); |
| 37 | datastore.namespace(namespace); |
| 38 | return datastore; |
| 39 | } |
| 40 | }); |