Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind jquery events in my view

Tags:

ember.js

I'd like to use some third party libraries that are based on jquery with ember. This library binds event on element like this :

$('#an-id')
    .bind('anEvent', function (event, params) { ... })

How do I catch the event enEvent into my Ember View and use an Ember based event handler. Something like :

App.MyView = Em.View.create({

   myEventHandler: function(event,params) { ....}
})
like image 338
jrabary Avatar asked Mar 16 '12 10:03

jrabary


People also ask

What is event binding in jQuery?

The jQuery bind() event is used to attach one or more event handlers for selected elements from a set of elements. It specifies a function to run when the event occurs. It is generally used together with other events of jQuery. Syntax: $(selector).

How do you bind a function to click an event in jQuery?

jQuery bind() MethodUse the on() method instead. The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.

What elements can you attach jQuery events to?

jQuery's event system requires that a DOM element allow attaching data via a property on the element, so that events can be tracked and delivered. The object , embed , and applet elements cannot attach data, and therefore cannot have jQuery events bound to them.

Which jQuery keyword should be used to bind an event to an element?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.


1 Answers

Use the didInsertElement callback on a view, which gets invoked when the views has been added to the DOM. You can then access the added element via this.$().

App.MyView = Em.View.create({
    didInsertElement: function() {
        this.$().bind('anEvent', ...);
    }
});
​
like image 75
pangratz Avatar answered Oct 10 '22 03:10

pangratz