.save()对现有模型造成的,而不是PUT POST [英] .save() on existing model causes POST instead of PUT

查看:90
本文介绍了.save()对现有模型造成的,而不是PUT POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,节省了集合中改变个别机型。已加载(通过集合.reset段())都发出POST(就好像它们是新的),而不是预期的PUT模型。

I'm having some trouble saving changes to individual models in a collection. Models that have been loaded (via a collection .reset()) are issuing a POST (as if they were new) instead of the expected PUT.

下面是我采用的方法:

APPVIEW


  • 通过加载this.model.childcollection.reset(JSON数据从服务器)子集;

  • Loads the child collection via this.model.childcollection.reset(JSON DATA FROM SERVER);

在它的渲染功能,集合中的每个项目创建一个新的childview,并使得它:

In it's render function, creates a new childview for each item in the collection and renders it:

render: function() {
        var el = this.el;
        this.model.childcollection.forEach(function(s) {
        var view = new ChildView({ model: s });
        el.append(view.render().el);
    });
    return this;
},


ChildView


  • 在它的事件,它是改变底层模型的一些价值观和调用save之一:

  • In one of its events it is changing some values of the underlying model and calling save:

this.model.set(
        {
            ValueA: somevalue,
            ValueB: somevalue
        },
        {
            error: function() {
                console.log("Error saving model");
            },
            success: function() {
                console.log("Model change saved");
            }
        });
    this.model.save();


观察:


  • POST(无子ID)被调用,而不是PUT(含子ID)

  • 子元素有ID设置

谁能告诉我,为什么这可能是发生?

Can anyone tell me why this may be happening?

推荐答案

骨干网使用的模型的 .ID 属性(属性不),以确定是否应该把或后,如下所示源$ C ​​$ C:<一href=\"https://github.com/documentcloud/backbone/blob/master/backbone.js#L344-346\">https://github.com/documentcloud/backbone/blob/master/backbone.js#L344-346

backbone used the .id property (not attribute) of the model to determine whether it should put or post, as shown here in the source code: https://github.com/documentcloud/backbone/blob/master/backbone.js#L344-346

如果把它保存现有模型时,正在做一个职位,这意味着未正确加载 .ID 属性。即使到 model.get通话(ID)返回正确的结果,为 model.id 通话必须返回正确的结果为它知道这是不是一个新的模式。

if it's doing a post when saving an existing model, this means the .id property wasn't loaded correctly. even if a call to model.get("id") returns the right result, a call to model.id must return the right result for it to know that this is not a new model.

要确保你的模型的id属性称为 ID ,如果不是的话,一定要设置 idAttribute 你的模型:

be sure you're model's id attribute is called id, or if it's not, be sure to set the idAttribute on your model:

MyModel = Backbone.Model.extend({
  idAttribute: "myCustomId"
});

这篇关于.save()对现有模型造成的,而不是PUT POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆