init: function() { this._super(); this._routes = [ ]; _.forEach(_.keys(this.routes), _.bind( function(uri) { this._parseRoute(uri, this.routes[uri]); }, this) ); // Listen for history.statechange events this.bind('_onstatechange'); if (History.enabled) { this.on(); } // Call any given initialize method if (typeof this.initialize === 'function') { this.initialize.apply(this, arguments); } // When we initialize a new router, we trigger a statechange event. This shouldn't // cause any issues, though, as we ignore statechanges that have the same url as // the current one cloak.$win.trigger('statechange'); },
def.create = function() { var inst = collection.create.apply(collection, arguments); _.each(_.keys(rules), function(rule) { inst[rule] = true; }); return inst; };
init: function(opts) { this._super(); this._routes = [ ]; this._opts = _.defaults(opts || { }, Router.defaults); if (this._opts.isTopLevel) { this.topLevel = this; } _.forEach(_.keys(this.routes), _.bind( function(uri) { this._parseRoute(uri, this.routes[uri]); }, this) ); this._subRouters = [ ]; this.bind('handleAnchor'); // Listen for history.statechange events this.bind('_onstatechange'); if (History.enabled && this._opts.autoStart) { this.start(); } // Call any given initialize method if (typeof this.initialize === 'function') { this.initialize.apply(this, arguments); } },
serialize: function(opts) { var self = this; var result = { }; opts = opts || { }; _.each(_.keys(self.attributes), function(key) { if (! opts.attrs || key === cloak.config.idKey || _.indexOf(opts.attrs, key) >= 0) { var value = self.attributes[key]; if (value instanceof Model || value instanceof Collection) { value = self.serializeChild(value, opts.deep); } result[key] = value; } }); return result; },
unserialize: function(data) { this.emit('unserialize'); var attrs = this.attributes; var origAttrs = this.constructor.prototype.attributes; _.each(_.keys(data), function(key) { var value = data[key]; // Is this field a model? if (Model.isModel(origAttrs[key])) { // These are the same model, just update the data if (attrs[key] instanceof Model && attrs[key].is(value)) { if (typeof value !== 'string') { return attrs[key].unserialize(value); } } // These are different, we need to replace the old one else { var Child = origAttrs[key]; if (typeof value === 'string') { value = cloak.idObj(value); } value = Child.create(value); } } // Is this field a collection? else if (Collection.isCollection(origAttrs[key])) { return attrs[key].unserialize(data[key]); } attrs[key] = value; }); // Empty out the changes list this._changedLocally.length = 0; this.emit('unserialized'); },
_buildAttributes: function() { var attrs = { }; var scope = this.constructor; var scopeAttrs; do { scopeAttrs = scope.prototype.attributes; if (scopeAttrs) { // Allow for functions that return attributes objects if (typeof scopeAttrs === 'function') { scopeAttrs = scopeAttrs.call(this); } // Extend the building attributes object with the new attributes attrs = _.extend(scopeAttrs, attrs); } } // Work our way up the prototype chain.. while ((scope = scope._parent) && scope !== Model); // Process the constructed attributes object for Models and Collections _.each(_.keys(attrs), _.bind(this._initializeModelsAndCollections, this, attrs)); return attrs; },
unbindEvents: function(events) { _.forEach(events || _.keys(this.events), _.bind(this._unbindEvent, this, this.events._delegate)); },