Skip to content

Commit

Permalink
Build model-list.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Apr 26, 2012
1 parent 8133cc0 commit ab9eadf
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
61 changes: 50 additions & 11 deletions build/model-list/model-list-debug.js
Expand Up @@ -230,6 +230,10 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
@param {Object} [options] Data to be mixed into the event facade of the
`add` event(s) for the added models.
@param {Number} [options.index] Index at which to insert the added
models. If not specified, the models will automatically be inserted
in the appropriate place according to the current sort order as
dictated by the `comparator()` method, if any.
@param {Boolean} [options.silent=false] If `true`, no `add` event(s)
will be fired.
Expand All @@ -239,8 +243,19 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
var isList = models._isYUIModelList;

if (isList || Lang.isArray(models)) {
return YArray.map(isList ? models.toArray() : models, function (model) {
return this._add(model, options);
return YArray.map(isList ? models.toArray() : models, function (model, index) {
var modelOptions = options || {};

// When an explicit insertion index is specified, ensure that
// the index is increased by one for each subsequent item in the
// array.
if ('index' in modelOptions) {
modelOptions = Y.merge(modelOptions, {
index: modelOptions.index + index
});
}

return this._add(model, modelOptions);
}, this);
} else {
return this._add(models, options);
Expand Down Expand Up @@ -408,7 +423,11 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {

if (options.asList) {
list = new Y.ModelList({model: this.model});
filtered.length && list.add(filtered, {silent: true});

if (filtered.length) {
list.add(filtered, {silent: true});
}

return list;
} else {
return filtered;
Expand Down Expand Up @@ -649,10 +668,12 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
/**
Removes the specified model or array of models from this list. You may also
pass another ModelList instance to remove all the models that are in both
that instance and this instance.
that instance and this instance, or pass numerical indices to remove the
models at those indices.
@method remove
@param {Model|Model[]|ModelList} models Models to remove.
@param {Model|Model[]|ModelList|Number|Number[]} models Models or indices of
models to remove.
@param {Object} [options] Data to be mixed into the event facade of the
`remove` event(s) for the removed models.
Expand All @@ -665,7 +686,18 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
var isList = models._isYUIModelList;

if (isList || Lang.isArray(models)) {
return YArray.map(isList ? models.toArray() : models, function (model) {
// We can't remove multiple models by index because the indices will
// change as we remove them, so we need to get the actual models
// first.
models = YArray.map(isList ? models.toArray() : models, function (model) {
if (Lang.isNumber(model)) {
return this.item(model);
}

return model;
}, this);

return YArray.map(models, function (model) {
return this._remove(model, options);
}, this);
} else {
Expand Down Expand Up @@ -904,7 +936,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
}

facade = Y.merge(options, {
index: options.index || this._findIndex(model),
index: 'index' in options ? options.index : this._findIndex(model),
model: model
});

Expand Down Expand Up @@ -1016,7 +1048,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
Removes the specified _model_ if it's in this list.
@method _remove
@param {Model} model Model to remove.
@param {Model|Number} model Model or index of the model to remove.
@param {Object} [options] Data to be mixed into the event facade of the
`remove` event for the removed model.
@param {Boolean} [options.silent=false] If `true`, no `remove` event will
Expand All @@ -1025,14 +1057,21 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
@protected
**/
_remove: function (model, options) {
var index = this.indexOf(model),
facade;
var index, facade;

options || (options = {});

if (index === -1) {
if (Lang.isNumber(model)) {
index = model;
model = this.item(index);
} else {
index = this.indexOf(model);
}

if (index === -1 || !model) {
this.fire(EVT_ERROR, {
error: 'Model is not in the list.',
index: index,
model: model,
src : 'remove'
});
Expand Down
2 changes: 1 addition & 1 deletion build/model-list/model-list-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab9eadf

Please sign in to comment.