Models

AJAX Abstraction for a Single Resource

isNew

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 to create or update the resource. You can read more about that here.

Default Implementation

The default implementation looks like this:

isNew: function() {
  return !this.has(this.idAttribute);
},

Usage

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.