【发布时间】:2013-01-04 18:51:07
【问题描述】:
我在 CoffeeScript 中使用 Backbone.js 进行编程,但在与 lodash 和合并函数相关的继承方面存在问题。
有一个超类
class NavigationView extends Backbone.View
config:
test:
string: "Test"
以及从它派生的两个类
class SubView extends NavigationView
initialize: ->
# Setting the view's template property using the Underscore template method
_.merge @config, {
test:
string: "bla"
}
class IndexView extends NavigationView
...
如果我在 SubView 的函数中更改初始化配置变量,它也会在 IndexView 的实例中更改。
我像这样在 BackBone.Router 类中实例化我的对象:
index: () ->
# Instantiates a new view which will render the header text to the page
new IndexView()
sub: () ->
new SubView()
我创建了一个小提琴来展示它:http://jsfiddle.net/hijolan/9VeND/
有什么办法吗?
最好的问候,hijolan
【问题讨论】:
-
您还有其他未向我们展示的内容。您的代码与this example 有何不同?您可能想检查最后一个代码块中的缩进,正确的缩进在 CoffeeScript 中非常重要。
-
嘿!你是绝对正确的......我使用 _.merge 来自 lodash 的函数(如 underscore.js)在我的初始化函数中合并配置以合并父配置和派生类中的配置......在将其替换为整个配置未合并(仅用于测试目的)它工作正常!感谢您的帮助...
-
以下小提琴显示了问题:jsfiddle.net/hijolan/9VeND
标签: inheritance backbone.js coffeescript lodash