Skitterm
Skitterm

Reputation: 4595

Backbone.js model sync--where set method?

I have written a custom Backbone.sync method that looks as follows:

sync: function(method, model, options) {
switch(method)
case 'create':
//do stuff here
}

Later on, I set everything up using the set() and call the sync by calling save(). Although I can set the backbone model's attributes, where can I set the method to 'create', 'delete'. etc?

Upvotes: 0

Views: 851

Answers (2)

jevakallio
jevakallio

Reputation: 35890

You don't need to specify the method argument to Backbone.sync yourself. The different methods are used in the following scenarios:

  • create - when you call model.save() on a new model (which does not have id)
  • update - when you call model.save() on an existing model (which has an id)
  • patch - when you call model.save(..., {patch:true})
  • delete - when you call model.destroy()
  • read - when you call model.fetch() or collection.fetch()

Upvotes: 3

cliffbarnes
cliffbarnes

Reputation: 1406

I am not sure exactly what you're doing here based off the code, but I am assuming you are calling an AJAX-like method with create and delete. If I am understanding you correctly, these methods should go in your views.

Upvotes: 0

Related Questions