0

So I have this model that gets populated via .fetch() my problem is. It will either always PUT, or always POST but never is .save() acting "smart".

The model is populated from a fetch, which if anything is returned from the fetch, its ever at most a single item, so running a collection for it is a bit abusive. Anyway.. I know that the my issue is in part related to my use of idAttribute if I use it, the .save() always does a PUT if I don't its always a POST.

my.Models.note = Backbone.Model.extend({
    idAttribute:'note',
    initialize: function (data) {
        this.user_id = data.user_id;
    },
    parse: function(response) {
        if (response.status == 'SUCCESS') {
            return response.data;
        }
    },

    url:function(data)
    {
        var url = '/notes/';
            url += '?user_id='+this.user_id;
        return url;
    }
});
chris
  • 36,115
  • 52
  • 143
  • 252
  • Is the value of `model.attributes.note` populated? If Backbone sees the id value then it uses PUT, otherwise it uses POST. http://stackoverflow.com/questions/11013049/backbone-save-post-instead-of-put – prototype Jun 12 '15 at 04:05
  • I understand why you'd link to that, and I've already viewed that. I am not creating a new model to save(). I am wanting to update the existing model that was previously fetched if there was data in that fetch – chris Jun 12 '15 at 04:07
  • Isn't PUT always appropriate for updating an existing model? Alternatively, if note is null, how would it know not to use POST? – prototype Jun 12 '15 at 04:09
  • Honestly I have been thinking about that `null` but its been put on the back burner for this notion. In the event there is data in the model and I am saying set that data to change it, then telling it to save that bit of data it should be dispatching a PUT to the server to update, rather it just runs a POST instead. So its always POST if I don't use an idAttribute in the model, and its always PUT if I do. – chris Jun 12 '15 at 04:15

2 Answers2

1

Its depends on your data , you should have id attribute in your data fetched from server, when you call save then automatically it sends a PUT call.if id is null then it send a POST call

0

Backbone uses the id attribute to determine if it's an existing resource (PUT) or a new resource (POST).

If you don't want to use the id attribute, you can override Backbone.Model.sync (rather than URL method) to specify the method that it should use:

my.Models.note = Backbone.Model.extend({
    idAttribute:'note',
    initialize: function (data) {
        this.user_id = data.user_id;
    },
    parse: function(response) {
        if (response.status == 'SUCCESS') {
            return response.data;
        }
    },
    sync: function(options) {
       $.ajax({ 
         type: 'PUT', 
         data: this.toJSON(),
         url:  '/notes/?user_id='+this.user_id, 
         success: options.success,
         error: options.error
       });
       return this;
    }
});
prototype
  • 7,249
  • 15
  • 60
  • 94