Backbone View extends 被污染 [英] Backbone View extends is polluted

查看:19
本文介绍了Backbone View extends 被污染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表模态的视图.这个视图有一些属性,比如页脚额外的类.我遇到了一个麻烦,当某些模式更改其值时,它们都收到相同的属性.

I have a view that represents a modal. This view has some attributes, like the footer extra class. I'm having a trouble that, when some modal change it's value, all of them receive the same attribute.

示例如下:

var ModalView = AlertingView.extend({
    className : "modal",
    paramsTemplate: { footerClass: "" }   
});

在那之后,我使用了具有更改属性的模态.

After that, I used the modal that has the changed attribute.

SpecialModal = ModalView.extends({
    initialize: function() {
       this.paramsTemplate.footerClass = "white";
    } 
})

所以,如果我实例化一个普通的Modal,这个参数就设置好了!:'(

So, if I instanciate a normal Modal, this parameter is set! :'(

NormalModal = ModalView.extends({
    initialize: function() {
       console.log(this.paramsTemplate.footerClass);//shows 'white'!
    } 
})

你知道如何解决它吗?谢谢!

Do you have any idea of how solving it? thanks!

推荐答案

http://jsfiddle.net/JQu5Q/11/

这个小提琴演示了你的问题.

This fiddle demonstrates your problem.

只要您没有创建 SpecialModal 的任何实例,NormalModal 的每个实例都可以工作.

As long as you've not created any instance of SpecialModal every instance of NormalModal works.

这是因为 paramsTemplate 是一个类变量,而不是一个实例变量.一旦创建了 SpecialModal 的实例,它就会更新 classVariable,因此,所有对象都会反映该更改.

This is because paramsTemplate is a class variable, not an instance variable. As soon as an instance of SpecialModal is created, it updates the classVariable and hence, all objects reflect that change.

幸运的是,几天前我刚刚遇到了类似的问题 Backbone.js 查看实例变量?

Fortunately, I've just had a similar issue few days back Backbone.js view instance variables?

解决方案:

http://jsfiddle.net/JQu5Q/12/

var ModalView = Backbone.View.extend({
    className : "modal",
    initialize:function(){
        this.paramsTemplate= { footerClass: "" }   ;
    }
});
SpecialModal = ModalView.extend({
    initialize: function() {
       ModalView.prototype.initialize.call(this);
       this.paramsTemplate.footerClass = "white";
        console.log(this.paramsTemplate.footerClass);
    } 
})
NormalModal = ModalView.extend({
    initialize: function() {
        ModalView.prototype.initialize.call(this);
       console.log(this.paramsTemplate.footerClass);
    } 
})

这篇关于Backbone View extends 被污染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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