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

前端 未结 2 1562
渐次进展
渐次进展 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:28

    Ember already has a "Global views hash"

    search ember.js for Ember.View.views = {}

    It is indexed by every views' element id, so getting a view is as easy as:

    myView = Ember.View.views[$('.myViewSelector').attr('id')]
    

    My mentor however believes using this method is a dirty hack and advised me to find a better way. He had written a test helper:

    window.lookupView = function(key) {
      var activeViews = app.__container__.lookup('router:main')._activeViews;
      if(!activeViews) { throw new Error("No active views."); }
      if(!activeViews[key]) { throw new Error("No view '%@'.".fmt(key)); }
      return activeViews[key][0];
    };
    

    which I used to get my desired parentView and then any children with parentView.get('childViews')

提交回复
热议问题