【发布时间】:2012-05-18 20:47:42
【问题描述】:
我有一个 HTML 元素绑定到我的视图模型中使用John Resig's simple javascript inheritance 构建的可观察对象之一。
<div data-bind="with: selectedItem()">
...
<div data-bind="click: $root.save">Save changes</div>
...
</div>
我的 ViewModel 看起来像这样。
var ViewModel = Class.extend({
init: function(type){
this.type = type;
this.selectedItem = ko.observable({name: "My name"});
},
save: function(data){
alert(this.type);
}
});
ViewModel#save 中的“this”引用了“selectedItem()”。 换句话说,“this”引用了传递给函数的“data”。 我如何才能访问 ViewModel 的instance?
已编辑
这里的目的是从 ViewModel 继承函数。我想分别访问“this.type”作为“first”和“second”。
var FirstViewModel = ViewModel.extend({
init : function() {
this._super('first');
}
});
var SecondViewModel = ViewModel.extend({
init : function() {
this._super('second');
}
});
【问题讨论】:
-
请参考John Resig的简单javascript继承link
-
我喜欢 John Resig 拒绝提及 mootools 的方式,或者 mootools 多年来一直这样做的事实。耶!
标签: javascript inheritance knockout.js