3

What is the difference between Backbone js model set and save method ?

var book = new Backbone.Model({
           title: "The Rough Riders",
           author: "Theodore Roosevelt"
});

book.save({author: "Teddy"});

book.set("title", "A Scandal in Bohemia");

As per the official backbone js documentation page it is described as saving a model into database by delegating into Backbone.sync. Is that the only difference between these methods?

Thanks,
Srinivas

Srinivas
  • 1,516
  • 3
  • 17
  • 27

2 Answers2

8

Yes, by using save you will delegate to Backbone.Sync and save your model data on the database, or local storage for example, it dependes on what you're using to persist your data. On the other hand with set you will simply update the model's attribute, triggering the "change" event and so on...

Ingro
  • 2,841
  • 5
  • 26
  • 42
2
book.set("title", "A Scandal in Bohemia");

The code will change book from DOM. save function will change from database, if you use first time to the object it will be created, second time it will be updated.

Ulug'bek
  • 2,762
  • 6
  • 31
  • 59