Collections
AJAX Abstraction for Lists of Resources
AJAX Abstraction for Lists of Resources
The reset()
method can be used to remove all models from the collection, or to replace the models with a new set.
The default implementation looks like this:
reset: function(models, options) {
options = options ? _.clone(options) : {};
this._reset();
models = this.add(models, options);
return models;
},
Let's save you've created a collection like this:
import { Collection } from 'lore-models';
const TweetCollection = Collection.extend({
url: 'http://localhost:1337/tweets'
})
const tweets = new TweetCollection([
{
id: 1,
text: 'Some old tweet'
}
])
At this point tweets.models.length
would return 1
, since we have 1 model.
Calling tweets.reset()
with no arguments will remove all models from the collection, and now tweets.models.length
would return 0
.
You can also invoke reset()
with arguments like this:
tweets.reset([
{
id: 2,
text: 'Some new tweet'
}
])
In this case, the method will first remove the existing models, and then add the new models. So after this method is called, tweets.models.length
would still return 1
, but if we examined tweet.models
we would see this, reflecting the array we just provied:
[
new Model({
id: 2,
text: 'Some new tweet'
})
]