微风迪朗达尔访问从视图shell变量视图模型 [英] Breeze Durandal Access shell viewmodel variable from view

查看:182
本文介绍了微风迪朗达尔访问从视图shell变量视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要完成一个pretty简单的事情,但我无法弄清楚它是如何,或者甚​​至有可能。我现在用热毛巾模板开始。在外壳视图模型我有一个用户可观察的。我希望能够引用该用户从我的网站上的其他网页观察到。比如从主页。我试了几件事情,但它doenst看起来好像我可以从由View访问外壳。我有一个使用事件发布/订阅电话从外壳传递用户数据给任何人听的那一刻起有效的解决方案,只要数据的变化(在这个例子中主视图)。这个工程它只是似乎有点麻烦,并没有真正处理这种理想的方式。此用户可观察将需要被整个网站的所有用于确定某些特征应提供和显示一个特定的用户的项目。

I have a pretty simple thing I want to accomplish but I cannot figure out how or if it is even possible. I am using the Hot Towel template to start with. In the shell viewmodel I have a user observable. I would like to be able to reference that user observable from other pages on my site. For example from the home page. I tried a couple of things but it doenst appear as though I can access the shell from the composed view. I have a working solution at the moment that uses event pub/sub calls from the shell to pass the user data to anyone listening whenever the data changes (home view in this example). This works it just seems a little clunky and not really the ideal way to handle this. This user observable will need to be used all throughout the site to determine when certain features should be available and to show a particular users projects.

有没有包含在从家里看法?

Is there a way to data bind to a knockout observable contained in the shell viewmodel from the home view?

推荐答案

您可以考虑具有返回一个单身,你考虑的型号包括根据需要在global.js。

You might consider having a global.js that returns a singleton, which you include in view models as needed.

define(function () {

    return {
        sharedObservable: ko.observable(),
        sharedObservableArray: ko.observableArray(),
        ...
    };
});

在一个视图模型使用全局。

Using global in a viewmodel.

define([..., global], function (..., global) {
    ...
    global.sharedObservable('updated');

    // As an alternative use a local var for easier access
    // var localVar = global.sharedObservable;
    // localVar('updated') 
    ...

});

这篇关于微风迪朗达尔访问从视图shell变量视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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