Backbone.js的继承意见 [英] Backbone.js Inherit Views
问题描述
我有处理此类事件的保存和取消一个FormView。我也有一个处理保存,取消和阿贾克斯获得形式的AjaxFormView。我有一个处理保存,取消,阿贾克斯获得,删除和打印的AssetFormView。
等等等等。有相当repitition。
I have a FormView which handles such events as save and cancel. I also have an AjaxFormView that handles save, cancel and get form by ajax. I have an AssetFormView that handles save, cancel, get by ajax, delete, and print. So on and so forth. there is considerable repitition.
我发现了一个帖子这个帖子的http://kalimotxocoding.blogspot.com/2011/03/playing-with-backbonejs-views.html
I found a post this post http://kalimotxocoding.blogspot.com/2011/03/playing-with-backbonejs-views.html
在那里他显示了可以扩展的意见。然而,我发现,当我有在页面上的看法多个版本有交叉性质授粉。有没有内置的方式来继承骨干意见,认为是安全的?
where he shows you can extend views. However, I'm finding that when i have multiple versions of views on the page there are properties cross pollinating. Is there no built in way to inherit views in backbone, that is safe?
谢谢,
RAIF
的 * 的嗯好了,这东西是pretty厚,我目前的异花授粉的问题可能是(可能是)我的一部分了一些错误的结果,但问题仍然有效,是有没有和岂不是有一个重要的特征,某种程度上继承看法?
* hmmm well, this stuff is pretty thick and my current cross pollination issue may be ( probably is ) the result of some error on my part, but the question still stands, is there not and would it not be an important feature to have, some way to inherit views?
推荐答案
我想看到的,当你说你的属性是跨pollenating你的意思。
I'd like to see what you mean when you say that your properties are cross-pollenating.
的 View.extend
机制工作得很好。请注意,虽然,你是延伸出的一个原型的新功能。与原型继承,原型股吧是对象的新实例。
The View.extend
mechanism works quite well. Do be aware, though, that you are extending one prototype with new functions. With prototypical inheritance, the prototype shares it's objects with the new instances.
我猜测,当你说你的属性是跨pollenating,实际上是在做这样的事情:
I am guessing that when you say that your properties are "cross-pollenating", you are actually doing something like this:
var baseModel = Backbone.Model.extend({
defaults: {
foo: { bar: "baz" }
}
});
由于对象是共享的,每个实例 baseModel
结束有相同的对象富
,给异花授粉的感觉。
Since the objects are shared, every instance of baseModel
ends up having the same object for foo
, giving the feeling of cross-pollination.
如果相反,你定义的默认值作为一个函数,那么每个实例都将获得它的富
对象的自己的副本,你的异花授粉消失。
If instead, you define your defaults as a function, then each instance will get it's own copy of the foo
object and your cross-pollination goes away.
var baseModel = Backbone.Model.extend({
defaults: function() { return {
foo: { bar: "baz" }
} }
});
当然,没有code,我们不能肯定你的问题是什么。只知道这个机制已经在社区中充分使用无故障。这是安全的。你只需要明白是怎么回事。
Of course, without code, we can't be certain to what your problem is. Just know that this mechanism has been well-used among the community without trouble. It is safe. You just need to understand what is going on.
这篇关于Backbone.js的继承意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!