mreq
mreq

Reputation: 6542

KineticJS: listening by .on()

With KineticJS, is it possible, to bind the function only once? Like with the jQuery equivalent...

eg. in jQuery

// bad
$('.wrap a').on('click', myHandler);
// good
$('.wrap').on('click', 'a', myHandler);

I would like to do this with KineticJS as well, as there's going to be a lot of objects.

Upvotes: 5

Views: 681

Answers (2)

Eric Rowell
Eric Rowell

Reputation: 5219

This is Eric from KineticJS. Currently, you can use container.get() to get an array of nodes similar to jQuery, like these:

layer.get('Rect'); // returns all rectangles inside of layer
layer.get('.foo'); // returns all nodes with name foo inside layer
stage.get('#bar'); // returns node with id 'bar'

but you cannot yet bind events to all of the nodes via the selector like this:

layer.get('Rect').on('click', ...);

I'll add this to my todo list, so it should probably be released within a couple weeks :)

Cheers!

Eric

Upvotes: 5

Stripps
Stripps

Reputation: 156

I dont know if there's any way to add a listner to all objects of a certain type (if thats what your trying to do).

Although you could make a group and add listeners to the whole group.

Fiddle: http://jsfiddle.net/9vsps/

Just notice that if you drag something in the group, everything in the group will be dragged.

Upvotes: 1

Related Questions