如何触发 model.save() 的成功回调?

发布于 2024-11-02 21:50:23 字数 217 浏览 1 评论 0原文

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

模型已正确发布到处理保存的服务器,但未触发成功回调。我需要从服务器发回一些东西吗?

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

The model is correctly posted to the server which handles the save, but the success callback is not fired. Do I need to send something back from the server ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

金橙橙 2024-11-09 21:50:23

save 的第一个参数是要保存在模型上的属性:

this.model.save( {att1 : "value"}, {success :handler1, error: handler2});

The first argument of save is the attributes to save on the model:

this.model.save( {att1 : "value"}, {success :handler1, error: handler2});
那请放手 2024-11-09 21:50:23

由于某些未知的原因,上述方法都不适合我。在我的例子中,只有 api 没有被命中。

但后来在搜索这个问题时,我遇到了 这个链接,其中有人尝试过 null 而不是 {} 作为第一个参数。

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

所以,这对我有用。希望这也对您有帮助。

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.

灰色世界里的红玫瑰 2024-11-09 21:50:23

您的服务器必须返回一个 JSON 对象。如果响应不是 JSON 对象,则不会触发回调。

如果要成功,您的服务器不返回 JSON 对象,请使用 dataType:"text" 选项执行保存,如下所示:

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

使用此选项,它不会等待 JSON 响应,而是等待文本,因此将启动回调。

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.

绝不放开 2024-11-09 21:50:23

您可以使用下划线库,如下所示,因为主干已经依赖于此。请记住,save 的第一个参数必须具有属性,或者如果您想保存模型本身,您可以只传递 {}。

this.model.save({}, _.bind(function(model, response){
  //Do whatever you want e.g.
  this.collection.add(model)
}, this))

You may use underscore lib as follows as backbone already depends upon this. Remember first argument of save must either have attributes or you may just pass {} in case you want to save model itself.

this.model.save({}, _.bind(function(model, response){
  //Do whatever you want e.g.
  this.collection.add(model)
}, this))
柒夜笙歌凉 2024-11-09 21:50:23

所以我有点困惑 - 我是否仍然需要传递所有属性才能调用保存事件?如果我的模型很大怎么办..我不想手动设置每个属性

,我调用 model.save 并尝试执行以下操作:

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

好的,只是为了回答我自己的问题,以防有人发现这篇文章,我做了以下有效的操作:

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

编辑:由于某种原因我无法回复你,但我可以编辑

,但你不必设置 id: this.model.get('id') 你可以只传递一个空白对象,因为空白属性不会扩展属性,也不执行任何操作:

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

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");
    }
});
日久见人心 2024-11-09 21:50:23

以下是我用于骨干模型保存的代码。

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

欢呼

罗伊·MJ

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

猫卆 2024-11-09 21:50:23

对于那些想要保存模型而不更新属性的人,您可以执行以下操作:

model.once("sync", function(model, response, options){
    //
});
model.once("error", function(model, response, options){
    //
});
model.save();

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();
大姐,你呐 2024-11-09 21:50:23

在初始化函数中,将同步方法绑定到您定义的方法(onSaveSuccess),

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

这样,每当您运行 this.model.save() 时,如果同步成功,它将运行 onSaveSuccess 函数作为回调

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文