Collections
AJAX Abstraction for Lists of Resources
AJAX Abstraction for Lists of Resources
The add()
method can be used to add models to the collection outside the constructor.
The default implementation looks like this:
add: function(models, options) {
return this.set(models, _.extend({}, options));
},
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()
The add()
method can be used add additional models to the collection, and there are two ways to call it.
The first is by providing a object, like this:
tweets.add({
id: 1,
text: 'Some new tweet'
})
The second is by providing an array, like this:
tweets.add([
{
id: 1,
text: 'Some new tweet'
}
])
In both cases, if you now examined the tweet.models
property you would see this:
[
new Model({
id: 1,
text: 'Some new tweet'
})
]