我知道如何从DOM中的Ember视图中构造jQuery对象:

App.myView = Ember.View.extend({
  elementId: "my_view",

  didInsertElement: function(){
    console.log(this.$().attr('id'))  // outputs 'my_view'
  }
});


<!-- HTML output -->

<div id="my_view">
   <!--  ...   -->
</div>

如何进行逆。给定一个jQuery对象,如何获得相应的Ember视图对象。

function(selector){
   $el = $(selector);
   // ???
}
有帮助吗?

解决方案

function getClosestEmberView($el) {
  var id = $el.closest('.ember-view').attr('id');
  if (!id) return;
  if (Ember.View.views.hasOwnProperty(id)) {
    return Ember.View.views[id];
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top