如何从静态JavaScript获取ember / emberjs中的视图实例的引用? [英] How to get a reference on a view instance in ember / emberjs from static javascript?

查看:139
本文介绍了如何从静态JavaScript获取ember / emberjs中的视图实例的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络上看到很多问题(SOF和Google),但到目前为止还没有明确的答案。

I have seen a lot of questions about that on the web (SOF and Google) but so far no clear answer to the issue.

我有一个通常的Ember应用程序与各种视图和控制器。我的一个视图有一个我想从静态上下文调用的实例方法。因此在正常的javascript文件中。我应该得到一个引用ember的观点来调用方法?

I have a usual Ember application with various views and controllers. One of my views has an instance method that I would like to call from a static context. Thus in a normal javascript file. I should I get a reference to the view instanciated by ember to call the method on ?

几行代码来说明我的问题:

A few lines of code to illustrate my issue :

在ApplicationView.js中: strong>

In ApplicationView.js :

App.ApplicationView = Em.View.extend({
    templateName: 'application',

    myInstanceMethod:function () {
       this.anotherInstanceMethod(some, params);
    },
    // ... more code
});

在MyUtils.js中

var myUtils = myUtils || {
    myMethod: function() {
        myApplicationViewInstance.myInstanceMethod();
    }
};


推荐答案

这是我个人的处理这个问题的方法。我正在使用Ember.View的didInsertElement将视图注册在中心位置。这对单身人士的看法效果很好。对于非单身人士的观点,人们必须开发更复杂的ViewRegistry。

This is my personal approach to this problem. I am using the "didInsertElement" of Ember.View to register the View in a central place. This works well for singleton views. For non-singleton views, one would have to develop a more sophisticated ViewRegistry.

Ember Part

var App = Ember.Application.create({
    viewRegistry : {
        applicationView : null
    },
});

App.ApplicationView = Ember.View.extend({
    templateName : 'application',
    didInsertElement : function(){
        App.set("viewRegistry.applicationView", this);
    }
});

在MyUtils.js中

var myUtils = myUtils || {
    myMethod: function() {
        App.get("viewRegistry.applicationView").myInstanceMethod();
    }
};

这篇关于如何从静态JavaScript获取ember / emberjs中的视图实例的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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