如何不让backbone.js使用保存响应作为模型属性

发布于 2024-12-13 21:11:42 字数 139 浏览 0 评论 0原文

因此,当我在后端保存模型时,我的 api 发回一个响应,告诉一切顺利,并以 json 格式给你一些其他指针。

我的问题是骨干认为我想使用该响应作为我的模型的属性并自动转储它们在模型属性中..

我只是将其保存在前端,不想再次保存属性。

So when I save a model on the backend, My api send back a response telling everything went fine and giving you some other pointers in json format

My problem is that backbone think I want to use that response as attributes of my model and automatically dump them in the model attributes..

I just saved it on the front-end and do not want to save the attributs again.

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

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

发布评论

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

评论(3

千秋岁 2024-12-20 21:11:42

这就是 Backbone.Model.parse 的用途。默认情况下,它只是一个传递,所以你不需要调用“super”。

假设您只关心返回的两个属性(id 和 foo),并且不关心其他任何内容:

var myModel = Backbone.Model.extend({

    parse : function(resp, xhr) {
        return {
            id:  resp.id,
            foo: resp.foo
        };
    }

});

请注意,我在示例中包含了“id”。创建(POST)返回一个 id 属性非常重要。没有它,Backbone 模型将来将不知道如何更新/删除。即使它有不同的 id 名称(例如“objectId”),您仍然应该在此函数中设置 id。

That is the what Backbone.Model.parse is for. By default, it is just a pass-through, so you don't need to call "super".

Let's say you only care about two properties that come back (id and foo) and you don't care about anything else:

var myModel = Backbone.Model.extend({

    parse : function(resp, xhr) {
        return {
            id:  resp.id,
            foo: resp.foo
        };
    }

});

Note that I included "id" in my example. It is really important that creates (POST) return an id property. Without it, the Backbone model won't know how to update/delete in the future. Even if it has a different name for id (like "objectId"), you should still set the id in this function.

哆啦不做梦 2024-12-20 21:11:42

事实上,这是默认行为,如果你想改变它,你必须覆盖一些 Backbone 函数。

查看 save 的实现方式,您有两个选择 - 要么覆盖模型的 save,要么覆盖 parse 以使其了解数据你正在发送。
http://documentcloud.github.com/backbone/docs/backbone.html #section-41

或者,您可以放弃在响应中发送“指针”,因为空响应意味着模型在保存后不会更改。

Indeed that's the default behaviour, and if you want to change it, you have to overwrite some Backbone functions.

Looking at how save is implemented, you have two options - either overwrite save for your model, or overwrite parse to make it aware of the data you are sending.
http://documentcloud.github.com/backbone/docs/backbone.html#section-41

Or, you could give up sending the 'pointers' in the response, because an empty response means that the model does not change after save.

泪冰清 2024-12-20 21:11:42

我有您遇到的确切问题。 Backbone 是一个非常年轻的框架,而且 JavaScript 确实是动态的。所以“解决问题有一千种方法”这句话在这里非常适用。

我认为更合适的方法是使用名为 Mixins 的东西。这就是我所做的:

define([
   'underscore',
   'backbone',
   'jquery'
], function (_, Backbone, $) {

return {
    parse: function(response, xhr){
        var data = response;
        if(response.response && response.response.status != 0){
            return {};
        }
        if(response.response && response.response.data)
        {
            data = _.first(response.response.data);
            if(typeof data == 'undefined'){
                data={};
            }

        }
        if(_.isFunction(this.postParse)){
            return this.postParse.call(this, data);
        }
        return data;
    }
}
});

如您所见,我重写了 Backbone.Model.parse 原型方法,并提出了我自己的方法来接收响应。并根据我的服务器的规范解析响应。在您的情况下,您将采取一切措施来理解服务器的响应。

完成基础工作后,指定模型就非常容易了:

var group = Backbone.Model.extend(
    _.extend({}, ModelMixin, {
        initialize:function () {
        }
    })
);
return group;

瞧!您需要编写的所有解析方法和检查都封装在 ModelMixin “超类”中。

I have the exact issue you are encountering. Backbone is a pretty young framework with the additional fact that javascript is really dynamic. So the saying that there are a thousand ways to solve a problem applies really well here.

I think a more suitable way to go about this is to employ something called Mixins. Here's what I did:

define([
   'underscore',
   'backbone',
   'jquery'
], function (_, Backbone, $) {

return {
    parse: function(response, xhr){
        var data = response;
        if(response.response && response.response.status != 0){
            return {};
        }
        if(response.response && response.response.data)
        {
            data = _.first(response.response.data);
            if(typeof data == 'undefined'){
                data={};
            }

        }
        if(_.isFunction(this.postParse)){
            return this.postParse.call(this, data);
        }
        return data;
    }
}
});

As you can see, I've overridden the Backbone.Model.parse prototype method and came up with my own that takes in a response. And parse the response according to my server's spec. In you case, you would implement whatever it takes to understand your server's response.

With the ground work out of the way, specifying a model is very easy:

var group = Backbone.Model.extend(
    _.extend({}, ModelMixin, {
        initialize:function () {
        }
    })
);
return group;

Voila! All the parse methods and checks that you need to write is encapsulated in that one ModelMixin "superclass".

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