Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Ember View from jQuery object

I know how to construct a jQuery object from an Ember view which is in the DOM:

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>

How about doing the inverse. Given a jQuery object, how do I get the corresponding Ember View object.

function(selector){
   $el = $(selector);
   // ???
}
like image 699
Panagiotis Panagi Avatar asked Feb 22 '12 15:02

Panagiotis Panagi


1 Answers

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];
  }
}
like image 193
Roy Daniels Avatar answered Oct 15 '22 10:10

Roy Daniels