在视图模型中使用自定义对象 Knockout
我从 Silverlight 转到 Knockout.js。当我在 Silverlight 中创建 ViewModel 时,我经常会遇到这样的情况:
public class MyViewModel : ViewModel
{
private MyCustomClass custom = null;
public MyCustomClass Custom
{
get { return custom; }
set { custom = value;
NotifyPropertyChanged("MyCustomClass");
}
}
}
<TextBox Text="{Binding Path=Custom.PropertyName, Mode=TwoWay}" />
但是,我不确定如何在 Knockout.js 中做同样的事情。目前,我有:
<input type="text" data-bind="value:propertyName" />
var viewModel = {
custom: {
propertyName:""
}
}
I am coming from Silverlight to Knockout.js. When I would create ViewModels in Silverlight, I would often times have something like this:
public class MyViewModel : ViewModel
{
private MyCustomClass custom = null;
public MyCustomClass Custom
{
get { return custom; }
set { custom = value;
NotifyPropertyChanged("MyCustomClass");
}
}
}
<TextBox Text="{Binding Path=Custom.PropertyName, Mode=TwoWay}" />
However, I'm not sure how to do the same kind of thing in Knockout.js. Currently, I have:
<input type="text" data-bind="value:propertyName" />
var viewModel = {
custom: {
propertyName:""
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当然可以执行您现有的操作,但您可能希望绑定到
value: custom.propertyName
,除非您已经使用模板或custom
将上下文更改为custom
code>with KO 1.3 中的绑定。执行此操作的典型方法是为自定义对象创建构造函数,例如:
You can certainly do what you are have there, but you would want to bind to
value: custom.propertyName
unless you have already changed context tocustom
using a template or thewith
binding in KO 1.3.A typical way to do this is to create constructor functions for your custom object like: