有没有办法使用kendo mvvm将不同视图模型的两个属性绑定到彼此?或者我是否必须自己编写更改事件的代码?
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
中显示几个内容,具体取决于用户是否登录。在谈论用户组和权限时,可以进一步采取措施。任何人都可以给我一个例子或其他方法,以防这不是正确的方法和/或可能是不好的做法?
答案 0 :(得分:2)
您好我认为中介模式可能就是您所寻找的,它非常适合在视图模型之间发送消息而不直接引用它们。 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中的任何属性等。
摘要:Mediator在viewModel之间传递消息。您要更改属性的viewModel,启动函数等注册到消息。然后,另一个viewModel可以向注册到该特定消息的所有类发送消息。
希望有所帮助。