Skip to content
Browse files

Add arrays of models at the correct indices; support 0 as index; tests.

  • Loading branch information...
1 parent 929472a commit b3da3c3b43c2b68b59554a37a98ffa371ff69a53 @rgrove committed Apr 26, 2012
Showing with 65 additions and 3 deletions.
  1. +3 −0 src/app/HISTORY.md
  2. +18 −3 src/app/js/model-list.js
  3. +44 −0 src/app/tests/model-list-test.js
View
3 src/app/HISTORY.md
@@ -6,6 +6,9 @@ App Framework Change History
### ModelList
+* The `add()` method now accepts an `index` option, which can be used to insert
+ the specified model(s) at a specific index in the list. [Greg Hinch]
+
* The `each()` and `some()` methods now iterate over a copy of the list, so it's
safe to remove a model during iteration. [Ticket #2531910]
View
21 src/app/js/model-list.js
@@ -228,6 +228,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.
@@ -237,8 +241,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);
@@ -902,7 +917,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
});
View
44 src/app/tests/model-list-test.js
@@ -135,6 +135,17 @@ modelListSuite.add(new Y.Test.Case({
Assert.areSame('foo', added.get('foo'));
},
+ 'add() should add a model to the list at the specified index': function () {
+ var list = this.createList(),
+ model = this.createModel();
+
+ list.add([{name: 'first'}, {name: 'second'}, {name: 'third'}]);
+ list.add(model, {index: 0});
+
+ Assert.areSame(4, list.size(), 'list should contain 4 items');
+ Assert.areSame(model, list.item(0), 'model should be inserted at index 0');
+ },
+
'add() should add an array of models to the list': function () {
var list = this.createList(),
models = [this.createModel(), this.createModel()],
@@ -151,6 +162,21 @@ modelListSuite.add(new Y.Test.Case({
Assert.areSame('bar', added[1].get('bar'));
},
+ 'add() should add an array of models to the list at the specified index': function () {
+ var list = this.createList(),
+ modelOne = this.createModel(),
+ modelTwo = this.createModel(),
+ modelThree = this.createModel();
+
+ list.add([{name: 'first'}, {name: 'second'}, {name: 'third'}]);
+ list.add([modelOne, modelTwo, modelThree], {index: 0});
+
+ Assert.areSame(6, list.size(), 'list should contain 6 items');
+ Assert.areSame(modelOne, list.item(0), 'modelOne should be inserted at index 0');
+ Assert.areSame(modelTwo, list.item(1), 'modelTwo should be inserted at index 1');
+ Assert.areSame(modelThree, list.item(2), 'modelThree should be inserted at index 2');
+ },
+
'add() should add models in another ModelList to the list': function () {
var list = this.createList(),
otherList = this.createList(),
@@ -164,6 +190,24 @@ modelListSuite.add(new Y.Test.Case({
Assert.areSame(otherList.item(1), list.item(1));
},
+ 'add() should add models in another ModelList to the list at the specified index': function () {
+ var list = this.createList(),
+ otherList = this.createList(),
+ modelOne = this.createModel(),
+ modelTwo = this.createModel(),
+ modelThree = this.createModel();
+
+ otherList.add([modelOne, modelTwo, modelThree]);
+
+ list.add([{name: 'first'}, {name: 'second'}, {name: 'third'}]);
+ list.add(otherList, {index: 0});
+
+ Assert.areSame(6, list.size(), 'list should contain 6 items');
+ Assert.areSame(modelOne, list.item(0), 'modelOne should be inserted at index 0');
+ Assert.areSame(modelTwo, list.item(1), 'modelTwo should be inserted at index 1');
+ Assert.areSame(modelThree, list.item(2), 'modelThree should be inserted at index 2');
+ },
+
'add() should support models created in other windows': function () {
var list = this.createList(),
iframe = document.getElementById('test-iframe'),

0 comments on commit b3da3c3

Please sign in to comment.
Something went wrong with that request. Please try again.