Created
February 27, 2015 17:35
-
-
Save jgable/79b1d421d0203e8b4739 to your computer and use it in GitHub Desktop.
FluxBone Store with Dispatcher helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dispatcher = require('./dispatcher'); | |
/** @class Store */ | |
var Store = Backbone.Collection.extend(/** @lends Store.prototype */ { | |
/** | |
* Instantiate a new store from the passed in models and options. Will attempt to load models | |
* from `getInitialData` if none are passed in. | |
* | |
* @constructor | |
* @param {Object[]} models - The models for this collection | |
* @param {Object} options - The options for this collection | |
*/ | |
constructor: function (models, options) { | |
models = models || this.getInitialData(); | |
return Backbone.Collection.apply(this, [models, options]); | |
}, | |
/** | |
* Initialize the store (called automatically on instantiation). Registers the dispatcherEvents | |
* and loads the store with | |
*/ | |
initialize: function () { | |
this.registerDispatcherEvents(); | |
}, | |
/** | |
* Get initial data for this collection if models aren't passed to the constructor. Usually used | |
* to load initial data from `window.PAGEDATA`. | |
* | |
* @example | |
* | |
* function () { | |
* return window.PAGEDATA && window.PAGEDATA.focusAreas; | |
* } | |
* | |
* @returns {Object[]} The models to load | |
*/ | |
getInitialData: _.noop, | |
/** | |
* Registers the dispatcherEvents hash | |
*/ | |
registerDispatcherEvents: function () { | |
// Allow using a function or an object | |
var events = _.result(this, 'dispatcherEvents'); | |
if (_.isUndefined(events) || _.isEmpty(events)) { | |
// Bug out early if no dispatcherEvents | |
return; | |
} | |
var registrations = {}; | |
_.each(events, function (funcOrName, actionType) { | |
// Quick sanity check for whether there is a problem with the function/name passed | |
if (!(_.isFunction(funcOrName) || _.isFunction(this[funcOrName]))) { | |
console.warn('dispatcherEvent: "' + funcOrName + '" is not a function'); | |
return; | |
} | |
if (_.isFunction(funcOrName)) { | |
registrations[actionType] = funcOrName; | |
} else { | |
registrations[actionType] = this[funcOrName]; | |
} | |
}.bind(this)); | |
this.dispatcherToken = dispatcher.register(registrations, this); | |
}, | |
/** | |
* Unregisters all the dispatcherEvents | |
*/ | |
unregisterDispatcherEvents: function () { | |
if (!this.dispatcherToken) { | |
return; | |
} | |
dispatcher.unregister(this.dispatcherToken); | |
this.dispatcherToken = undefined; | |
}, | |
/** | |
* Get all models in plain objects. | |
* | |
* @returns {Object[]} All models as plain JSON | |
*/ | |
getAll: function () { | |
return this.toJSON(); | |
}, | |
/** | |
* Get a model by id | |
* | |
* @param {string} id - Id of the model to retrieve | |
* @returns {Object} - A plain JSON version of the model | |
*/ | |
getSingle: function (id) { | |
var found = this.get(id); | |
if (found) { | |
return found.toJSON(); | |
} | |
} | |
}); | |
module.exports = Store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment