How to get a reference on a view instance in ember / emberjs from static javascript?

前端 未结 2 1539
渐次进展
渐次进展 2021-01-04 22:50

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

I have a usual Ember application with various views

2条回答
  •  孤街浪徒
    2021-01-04 23:27

    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);
        }
    });
    

    In MyUtils.js:

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

提交回复
热议问题