将 Base2 与 KnockoutJS 视图模型结合使用
我使用 Base2 作为一种方法,让我们能够轻松地在系统中进行继承,并使用 KnockoutJS 进行一些 UI 交互。
我们已经为我们的 ViewModel 定义了一个基类,
BaseViewModel = Base.extend({
...
});
然后我们为我们的视图模型扩展了它:
ExampleViewModel = BaseViewModel.extend({
text: ko.observable("")
});
但是似乎有一个问题。当您创建视图模型的 2 个以上实例时(例如,如果您将它们推入 observableArray 并使用模板构建 UI),似乎对绑定字段所做的任何更改都会更新所有视图模型,而不仅仅是一个视图模型这是必然的。
有谁知道为什么会这样?
I am using Base2 as a means to allow us to easily do inheritance in our system, aswell as using KnockoutJS for some UI interactions.
We have defined a base class for our ViewModels
BaseViewModel = Base.extend({
...
});
Which we then extend for our view models:
ExampleViewModel = BaseViewModel.extend({
text: ko.observable("")
});
However there seems to be a problem. When you create 2+ instances of the view model (say if you are pushing them in to an observableArray and using templates to build up a UI) it seems like any changes made to a bound field, updates all view models rather than just the one it's bound to.
Does anybody know why this might be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为扩展实际上并没有实例化一个新的可观察对象,它只是复制引用。
我认为你可以这样做:
虽然不像普通的 Base2 语法那么好,但由于属性问题,只是限制了 Knockout 的实现方式。
Because the extension is not actually instantiating a new observable, its just copying the reference.
I think you can do something like this:
Not as nice though as normal Base2 syntax, but just a limitation in how Knockout is implemented due to issues with properties.