How do I trigger the success callback on a model.save()?

前端 未结 8 997
粉色の甜心
粉色の甜心 2020-11-28 23:06
this.model.save({
  success: function(model, response){
    console.log(\'success\');
  },
  error: function(){
    console.log(\'error\');
  }
})

相关标签:
8条回答
  • 2020-11-28 23:23

    For some unknown reason, none of the above method worked for me. The api only was not hit in my case.

    But later while searching on this, I bumped into this link, where some one had tried null instead of {} as the first parameter.

    this.model.save(null, {
        success: function (model, response) {
            console.log("success");
        },
        error: function (model, response) {
            console.log("error");
        }
    });
    

    so, this worked for me. Hope this helps you too.

    0 讨论(0)
  • 2020-11-28 23:29

    so im a little confused - do i still need to pass in all attributes in order for me to call a save event? what if my model is large.. i dont wish to set every property manually

    im calling model.save and attempting to do the following:

    this.model.save(
        {
            success: function (model, response) {
                console.log('model saved');
            }
        });
    

    ok just to answer my own question incase anyone finds this post, i did the following which works:

    this.model.save({ id: this.model.get('id') },
        {
            success: function (model, response) {
                console.log("success");
            },
            error: function (model, response) {
                console.log("error");
            }
        });
    

    EDIT: I couldn't reply to you for some reason, but I can edit

    but you don't have to set id: this.model.get('id') you can just pass a blank object because a blank attribute just won't extend attributes, does nothing:

    this.model.save({}, {
        success: function (model, response) {
            console.log("success");
        },
        error: function (model, response) {
            console.log("error");
        }
    });
    
    0 讨论(0)
  • 2020-11-28 23:33

    The following is the code that i am using for backbone model save.

    this.model.save(model,{
       success:function(model){
           console.log("Saved Successfully");
       },
       error:function(model){
           console.log("Error");
       }
    });
    

    Cheers

    Roy M J

    0 讨论(0)
  • 2020-11-28 23:36

    For those that want to save a model, without updating the attributes, you can do the following:

    model.once("sync", function(model, response, options){
        //
    });
    model.once("error", function(model, response, options){
        //
    });
    model.save();
    
    0 讨论(0)
  • 2020-11-28 23:43

    Your server must return a JSON object. If the response is not a JSON object, the callbacks will not fire.

    If for success your server doesn't return a JSON object, perform a save with dataType:"text" option, like this:

    this.model.save([],{
     dataType:"text",
     success:function() {},
     error:function() {}
    });
    

    With this option it will not be waiting for a JSON in response, but a text, and thus the callback will be launched.

    0 讨论(0)
  • 2020-11-28 23:46

    In you initialize function, bind the sync method to a method you define (onSaveSuccess)

                initialize: function (options) {
                        this.model.on('sync', _.bind(this.onSaveSuccess, this));
    },
                onSaveSuccess: function() {
                    console.log('saved');
                    this.render();
                },
    

    This way, any time you run this.model.save(), it will run the onSaveSuccess function as a callback if your sync is successful

    0 讨论(0)
提交回复
热议问题