Skip to content

Creating new records

Nikos Zinas edited this page May 17, 2014 · 1 revision

Creating new records is simple, you can instantiate a new object and assign properties in 3 ways. In the end, you must call the save() function to persist the data to the server.

var car = new Car();
car.brand = "Seat";
car.model = "Ibiza";
car.cost = 15000;
car.save();

// or

var car = new Car({
  brand : "Seat",
  model : "Ibiza",
  cost : 15000
});
car.save();

// or

var car = new Car();
car.values = {
  brand : "Seat",
  model : "Ibiza",
  cost : 15000
};
car.save();

The execution order of save() is: the beforeSave() hook is executed first, then the validations and then the actual call to the server.

A promise is returned, so you can attach your own callbacks

car.save().done(function () {
  // this will execute when the save is completed 
});