有没有办法使用kendo mvvm将不同视图模型的两个属性相互绑定,还是我必须自己编写change事件的代码?
UserViewModel = {
init: function(e) {
},
show: function(e) {
},
model: {
isLoggedIn: kendo.observable(false)
}
}
OtherContextViewModel = {
init: function(e) {
},
show: function(e) {
},
model: {
UserIsLoggedIn: //bind to isLoggedIn of the UserViewModel
}
}
我尝试这样做的原因:我想OtherContext
根据用户是否登录来隐藏和显示几件事。在讨论用户组和特权时,可以采取进一步的措施。如果这不是正确的方法和/或不好的做法,谁能给我一个例子或其他方法?
嗨,我认为调解器模式可能就是您要寻找的,它是在视图模型之间发送消息而无需直接引用它们的理想选择。CodeProject的教程显示了如何实现调解器模式。
http://www.codeproject.com/Articles/35277/MVVM-Mediator-Pattern
假设我们有2个ViewModel:1. LoginViewModel 2. MainViewModel
我们将MainViewModel注册到调解器消息UserLoggedIn
Mediator.Instance.Register(
(Object o) =>
{
UpdateView(o as loggedInBoolean);
}, Mediator.ViewModelMessages.UserLoggedIn);
当用户通过LoginViewModel登录时,我们会向中介者消息UserLoggedIn发送一条消息。
public void LoggingIn()
{
Mediator.Instance.Notify(Mediator.ViewModelMessages.UserLoggedIn, null);
}
发送此消息将触发UpdateView(loggedInBoolean)方法,您可以使用该方法更改MainViewModel中的任何属性等。
摘要:介体在viewModel之间传递消息。您想要更改属性,启动函数等的viewModel注册到消息中。然后,另一个viewModel可以将消息发送到已注册到该特定消息的所有类。
希望能有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句