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

前端 未结 8 1023
粉色の甜心
粉色の甜心 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: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");
        }
    });
    

提交回复
热议问题