|
| 1 | +import _ from 'underscore'; |
| 2 | +import Backbone from 'backbone'; |
| 3 | + |
| 4 | +/* |
| 5 | + * Encapsulates a cache for a single model. |
| 6 | + */ |
| 7 | + |
| 8 | +function ModelCache(Model, modelName) { |
| 9 | + this.instances = {}; |
| 10 | + this.Model = Model; |
| 11 | + this.modelName = modelName; |
| 12 | + this.ModelConstructor = this._getConstructor(Model); |
| 13 | +} |
| 14 | + |
| 15 | +_.extend(ModelCache.prototype, { |
| 16 | + _getConstructor: function _getConstructor(Model) { |
| 17 | + var cache = this; |
| 18 | + |
| 19 | + var ModelConstructor = function ModelConstructor(attrs, options) { |
| 20 | + return cache.get(attrs, options); |
| 21 | + }; // Extend Model's static properties onto new |
| 22 | + |
| 23 | + |
| 24 | + _.extend(ModelConstructor, Model); // Backbone collections need prototype of wrapped class |
| 25 | + |
| 26 | + |
| 27 | + ModelConstructor.prototype = this.Model.prototype; |
| 28 | + return ModelConstructor; |
| 29 | + }, |
| 30 | + get: function get(attrs, options) { |
| 31 | + var instanceId = attrs && attrs[this.Model.prototype.idAttribute]; // Attempt to restore a locally cached instance |
| 32 | + |
| 33 | + var instance = this.instances[instanceId]; |
| 34 | + |
| 35 | + if (!instance) { |
| 36 | + // If we haven't seen this instance before, start caching it |
| 37 | + return this._new(attrs, options); |
| 38 | + } // Otherwise update the attributes of the cached instance |
| 39 | + |
| 40 | + |
| 41 | + instance.set(attrs); |
| 42 | + Store.trigger('update', instance, this); |
| 43 | + return instance; |
| 44 | + }, |
| 45 | + _new: function _new(attrs, options) { |
| 46 | + var instance = new this.Model(attrs, options); |
| 47 | + |
| 48 | + if (instance.isNew()) { |
| 49 | + // Store the instance if we get an id after instantation |
| 50 | + instance.once("change:".concat(instance.idAttribute), this._add, this); |
| 51 | + } else { |
| 52 | + this._add(instance); |
| 53 | + } |
| 54 | + |
| 55 | + instance.on('destroy', this.remove, this); |
| 56 | + return instance; |
| 57 | + }, |
| 58 | + _add: function _add(instance) { |
| 59 | + // If the id is already stored do not add it. |
| 60 | + if (this.instances[instance.id]) return; |
| 61 | + this.instances[instance.id] = instance; |
| 62 | + Store.trigger('add', instance, this); |
| 63 | + }, |
| 64 | + remove: function remove(instance) { |
| 65 | + if (!this.instances[instance.id]) return instance; |
| 66 | + delete this.instances[instance.id]; |
| 67 | + Store.trigger('remove', instance, this); |
| 68 | + return instance; |
| 69 | + } |
| 70 | +}); |
| 71 | + |
| 72 | +var ModelCaches = {}; |
| 73 | +/** |
| 74 | + * Store wrapper converts regular Backbone models into unique ones. |
| 75 | + * |
| 76 | + * Example: |
| 77 | + * const StoredUser = Store(User); |
| 78 | + */ |
| 79 | + |
| 80 | +function Store(Model) { |
| 81 | + var modelName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _.uniqueId('Store_'); |
| 82 | + var cache = Store.add(Model, modelName); |
| 83 | + return cache.ModelConstructor; |
| 84 | +} // Static functions |
| 85 | + |
| 86 | + |
| 87 | +_.extend(Store, Backbone.Events, { |
| 88 | + ModelCache: ModelCache, |
| 89 | + add: function add(Model, modelName) { |
| 90 | + if (!modelName) throw 'Model name required'; |
| 91 | + if (ModelCaches[modelName]) return ModelCaches[modelName]; |
| 92 | + return ModelCaches[modelName] = new Store.ModelCache(Model, modelName); |
| 93 | + }, |
| 94 | + getCache: function getCache(modelName) { |
| 95 | + if (!ModelCaches[modelName]) throw "Unrecognized Model: \"".concat(modelName, "\""); |
| 96 | + return ModelCaches[modelName]; |
| 97 | + }, |
| 98 | + getAllCache: function getAllCache() { |
| 99 | + return _.clone(ModelCaches); |
| 100 | + }, |
| 101 | + get: function get(modelName) { |
| 102 | + return Store.getCache(modelName).ModelConstructor; |
| 103 | + }, |
| 104 | + getAll: function getAll() { |
| 105 | + return _.reduce(ModelCaches, function (all, cache, modelName) { |
| 106 | + all[modelName] = cache.ModelConstructor; |
| 107 | + return all; |
| 108 | + }, {}); |
| 109 | + }, |
| 110 | + remove: function remove(modelName) { |
| 111 | + delete ModelCaches[modelName]; |
| 112 | + }, |
| 113 | + removeAll: function removeAll() { |
| 114 | + ModelCaches = {}; |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +Backbone.Store = Store; |
| 119 | + |
| 120 | +export default Store; |
| 121 | +export { ModelCache }; |
0 commit comments