如何在多个viewModel中使用剑道可观察属性 [英] How to use a kendo observable property in multiple viewModels

查看:64
本文介绍了如何在多个viewModel中使用剑道可观察属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Kendo MVVM框架的Kendo应用程序中:我有一个全局" viewModel,它是应用程序所有部分共有的信息-例如UserState,其属性为isLoggedIn.

In a Kendo app using the Kendo MVVM framework: I have a "global" viewModel which is information that is common to all parts of the app - e.g. the UserState, which has a property isLoggedIn.

许多不同的View和ViewModel访问userState对象(据我所见,在Kendo中1 View绑定到1 ViewModel).

Many different Views and ViewModels access the userState object (from what I can see, 1 View is bound to 1 ViewModel in Kendo).

例如,如果我的主页未经身份验证,则可能会显示登录"按钮.然后,一旦您登录,所有其他屏幕的行为都会有所不同,因此每个ViewModel需要引用UserState对象.但是,如果其中任何一个发生更改,则当我使用Kendo Observable对象时,所有其他视图都应更新.这似乎不起作用.

For example, my home page might show the Login button if they are not authenticated. Then all the other screens behave differently once you are logged in, so each ViewModel needs to reference the UserState object. However, if any of them change it then all other Views should update as I used a Kendo Observable object. This does not seem to work.

我在这里设置了一个简单的示例来说明问题: http://jsfiddle.net/rodneyjoyce/uz7ph/4/

I set up a simple example here to illustrate the problem: http://jsfiddle.net/rodneyjoyce/uz7ph/4/

var app = new kendo.mobile.Application();   

var userStateviewModel = kendo.observable({
    isLoggedIn: false,
});

var viewModel1 = kendo.observable({
    label: 'From ViewModel1',
    isLoggedInVM1: userStateviewModel.isLoggedIn    
});

userStateviewModel.set('isLoggedIn', true);
//viewModel1.set('isLoggedInVM1', userStateviewModel.isLoggedIn);

alert(viewModel1.isLoggedInVM1);

kendo.bind($("#testForm"), viewModel1);

userStateViewModel被许多不同的视图使用,因此在ViewModel1中,我公开了iSloggedIn属性并绑定到视图中(以false开头).

userStateViewModel is used by many different Views, so in ViewModel1 I expose the iSloggedIn property and bind to it in the view (starts off as false).

然后代码在userStateViewModel中将其设置为true-因此我需要所有观察实例将其更改为true-但这不起作用.如果我注释掉这行代码 //viewModel1.set('isLoggedInVM1',userStateviewModel.isLoggedIn);

Then the code sets it to true in userStateViewModel - so I need all observiing instances of it to change to true - but this does not work. If I comment out this line of code //viewModel1.set('isLoggedInVM1', userStateviewModel.isLoggedIn);

然后它可以工作,但是这超出了一个可观察的角度,因为我不知道10个ViewModel中的哪个会更新它以及何时更新,但是所有视图都需要更新新值...

then it will work, but this defeats the point of an observable as I don't know which of the 10 ViewModels will update it and when, but all views need to update the new value...

推荐答案

viewModel1初始化中执行isLoggedInVM1: userStateviewModel.isLoggedIn时,您将分配它在该精确时间点(false)的值.而是使用获取值的函数.

When you do isLoggedInVM1: userStateviewModel.isLoggedIn in viewModel1 initialization, you are assigning the value that it has at that precise moment in time (false). Instead, use a function that gets the value.

var viewModel1 = kendo.observable({
    label: 'From ViewModel1',
    isLoggedInVM1: function() {
        return userStateviewModel.get("isLoggedIn");
    }
});

然后,当尝试检查isLoggedInVM1时执行:

Then, when trying to check isLoggedInVM1 do:

alert(viewModel1.isLoggedInVM1());

<h3 data-bind="text: isLoggedInVM1()"></h3>

您的JSFiddle在此处进行了修改: http://jsfiddle.net/OnaBai/uz7ph/6/

Your JSFiddle modified here: http://jsfiddle.net/OnaBai/uz7ph/6/

这篇关于如何在多个viewModel中使用剑道可观察属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆