Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fabric.js assign event listener to an object for multiple events

I am asking this mainly out of curiosity as it is possible to replicate code but being deep in rails at the minute, repetition makes me feel dirty and I can't find it in the docs.

Suppose I have a rect = fabric.Rect and I wanted to add a listener for say, moving and modified that do the same thing:

rect.on('moving', function() {
    console.log('moving or modified'); 
});

rect.on('modified', function() {
    console.log('moving or modified'); 
});

is it possible to combine these in some way?

I'm very new to JS and so this could be a simple thing in JS that I've not come across yet but again, it's not mentioned in Fabric docs that I've seen.

like image 568
DazBaldwin Avatar asked Apr 19 '14 10:04

DazBaldwin


1 Answers

I'm a little late, but just in case anyone else has this problem - you just need to move your common code into a function, and then pass key/value pairs to the on() function:

function doLog() {
    console.log('moving or modified');
}

rect.on({
    'moving': doLog,
    'modified': doLog
});
like image 176
John M Avatar answered Sep 22 '22 15:09

John M