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

前端 未结 8 1001
粉色の甜心
粉色の甜心 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: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

提交回复
热议问题