Backbone.js的Collection.create和覆盖Model.set [英] Backbone.js Collection.create and overridden Model.set

查看:168
本文介绍了Backbone.js的Collection.create和覆盖Model.set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在那里我已经改变了一套方法,对每一套模型计算额外的属性骨干模式。

I have a backbone model where I have changed the set method to calculate extra attributes on every set of the model.

根据文档,这应该是叫超级为了确保该模型实际上保存的方式。

According to the docs this should be the way to call super in order to make sure the model is actually saved.

Backbone.Model.prototype.set.call(this, attributes, options);

和它的工作原理就像预期的那样,除非我用Collection.create。

And it works just as expected, unless I use Collection.create.

我的组自定义的方法获取运行,但我觉得原本并不因为收藏品仍然是空的。
服务器接收正确的数据和右数据发送回。在这两次我的方法被执行,但收集仍然是空的。

My custom set method gets run, but I think the original does not since the collection remains empty. The server receives the correct data and sends the right data back. On both occasions my method gets executed, but the collection is still empty.

我可以修改超级呼叫,使其与Collection.create工作,或者是有另一种方式做同样的事情?

Can I change the super call to make it work with Collection.create or is there another way to do the same thing?

推荐答案

正当我以为,我错过了什么。
当重写Model.set()必须把返回这一点;

Just as I thought, I missed something. When overriding Model.set() one must put return this; at the end.

因此​​,一个模型应该是这样的:

So a model should look like this:

var MyModel = Backbone.Model.extend({
    set: function(attributes, options) {
        // Custom code...
        return Backbone.Model.prototype.set.call(this, attributes, options);
    }
});

这篇关于Backbone.js的Collection.create和覆盖Model.set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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