Models
AJAX Abstraction for a Single Resource
AJAX Abstraction for a Single Resource
The isNew()
method returns true or false depending on whether or not the model has an id
, which is determined by whether there is an attribute with a name that matches the idAttribute
.
This method is used by
save()
to determine whether tocreate
orupdate
the resource. You can read more about that here.
The default implementation looks like this:
isNew: function() {
return !this.has(this.idAttribute);
},
Let's use this code to illustrate:
import { Model } from 'lore-models';
const Tweet = Model.extend();
const t1 = new Tweet();
const t2 = new Tweet({
id: 2
});
In the code above, t2.isNew()
will return true
, because it has no id
, whereas t2.isNew()
would return false
because it does has an id
.
Underneath, this method uses idAttribute
to determine whether the model has an id
. For example, take a look at this code:
import { Model } from 'lore-models';
const Tweet = Model.extend({
idAttribute: '_id'
});
const t1 = new Tweet({
_id: 1
});
const t2 = new Tweet({
id: 2
});
In this code, t1.isNew()
will now return false
, because we defined the idAttribute
to be _id
, and it has one. And now t2.isNew()
would return true
because it does not have an id
, according to the idAttribute
.