base.js | |
---|---|
Base Collection | |
Requires | define(['facade', 'utils'], function (facade, utils) {
var BaseCollection,
Backbone = facade.Backbone,
$ = facade.$,
_ = facade._,
lib = utils.lib,
ajaxOptions = utils.ajaxOptions,
debug = utils.debug; |
Constructor | BaseCollection = Backbone.Collection.extend({ |
Method: | initialize: function (models, options) {
debug.log("BaseCollection initialize...");
this.cid = this.cid || _.uniqueId('c');
this.deferred = new $.Deferred(); |
When overriding use: | }, |
Property: | request: null,
_idAttr: 'id', |
Method: | fetch: function (options) {
options = options || {};
if (!options.success) {
options.success = this.fetchSuccess;
}
if (!options.error) {
options.error = this.fetchError;
}
_.extend(options, ajaxOptions);
this.request = Backbone.Collection.prototype.fetch.call(this, options);
if (!this.request) {
this.request = this.deferred.promise();
}
return this.request;
}, |
Primarily a tool for unit tests... Don't rely on calling this.isReady!! | isReady: function () {
if (this.request) {
return !!(this.request.state() === 'resolved');
} else {
return !!(this.deferred.state() === 'resolved');
}
}, |
Default success and error handlers used with this.fetch() ... | |
Method: | fetchSuccess: function (collection, response) {
this.deferred.resolve(response);
debug.log(response);
}, |
Method: | fetchError: function (collection, response) {
debug.log(response);
}
});
return BaseCollection;
});
|