Skip to content

Instantly share code, notes, and snippets.

@ericf
Created May 3, 2011 17:30
Show Gist options
  • Select an option

  • Save ericf/953781 to your computer and use it in GitHub Desktop.

Select an option

Save ericf/953781 to your computer and use it in GitHub Desktop.
/**
* Model Collection
*
* @module model
* @submodule model-collection
*/
var ModelCollection,
MODEL_COLLECTION = 'modelCollection',
noop = function(){},
YLang = Y.Lang,
isObject = YLang.isObject,
isArray = YLang.isArray,
isString = YLang.isString,
isFunction = YLang.isFunction;
// *** Model Collection *** //
ModelCollection = function (config) {
config = isArray(config) ? { models: config } : config;
var c = this.constructor,
name = c.NAME,
models;
// Setup Y.EventTarget
Y.EventTarget.call(this, {
prefix : name,
emitFacade : true,
queuable : true,
bubbles : true
});
// Setup Y.PluginHost
Y.Plugin.Host.call(this);
this.name = name;
Y.stamp(this);
models = this._initModels((config || {}).models);
ModelCollection.superclass.constructor.call(this, models);
this.initializer(config);
this._initPlugins(config);
};
Y.extend(ModelCollection, Y.ArrayList, {
// *** Prototype *** //
model : Y.Model,
// *** Lifecycle Methods *** //
initializer : noop,
destructor : noop,
// *** Public Methods *** //
create : function () {},
// *** Private Methods *** //
_initModels : function (models) {
models = Y.Array(models);
var initedModels = [],
byId = this._byId || (this._byId = {}),
model, i, len;
for (i = 0, len = models.length; i < len; i++) {
model = models[i];
(model instanceof Y.Model) || (model = new this.model(model));
/*
if (model._id) {
byId[model._id] = model;
}
*/
model.addTarget(this);
initedModels.push(model);
}
return initedModels.length > 1 ? initedModels : initedModels[0];
},
_detachModels : function (models) {
models = models ? Y.Array(models) : this._items;
var i, len;
for (i = 0, len = models.length; i < len; i++) {
models[i].removeTarget(this);
}
},
_reset : function () {
this._detachModels();
this._items = [];
},
_defAddFn : function (e) {
var args = Y.Array(e.args);
args[0] = this._initModels(args[0]);
ModelCollection.superclass.add.apply(this, args);
},
_defRemoveFn : function (e) {
this._detachModels(e.args[0]);
ModelCollection.superclass.remove.apply(this, e.args);
},
_defRefreshFn : function (e) {
var models = e.args[0];
this._reset();
this._items = this._initModels(models);
},
}, {
// *** Static *** //
NAME : MODEL_COLLECTION,
create : function (name, px, sx) {
function BuiltModelCollection () {
BuiltModelCollection.superclass.constructor.apply(this, arguments);
}
BuiltModelCollection.NAME = name;
if (isFunction(px)) {
px = { model: px };
}
return Y.extend(BuiltModelCollection, ModelCollection, px, sx);
},
plug : Y.Plugin.Host.plug,
unplug : Y.Plugin.Host.unplug,
eventWrap : function (event, config) {
var proto = this.prototype;
proto[event] = function(){
if (isString(config)) { config = isFunction(this[config]) ? this[config] : null; }
if (isFunction(config)) { config = { defaultFn: config }; }
this.publish(event, config);
function fireEvent () {
this.fire(event, { args: Y.Array(arguments) });
return this;
}
proto[event] = fireEvent;
return fireEvent.apply(this, arguments);
};
}
});
ModelCollection.eventWrap('fetch');
ModelCollection.eventWrap('add', '_defAddFn');
ModelCollection.eventWrap('remove', '_defRemoveFn');
ModelCollection.eventWrap('refresh', '_defRefreshFn');
Y.mix(ModelCollection, Y.EventTarget, false, null, 1);
Y.mix(ModelCollection, Y.Plugin.Host, false, null, 1);
// *** Namespace *** //
Y.ModelCollection = ModelCollection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment