admin 管理员组

文章数量: 1086019

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 bine these in some way?

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

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 bine these in some way?

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

Share Improve this question asked Apr 19, 2014 at 10:45 DazBaldwinDazBaldwin 4,3233 gold badges43 silver badges44 bronze badges 3
  • use only one why you want to bine? – Yogesh Commented Sep 1, 2014 at 14:27
  • 2 because they're two distinct events that may need the same action being performed when they are triggered. – DazBaldwin Commented Sep 2, 2014 at 11:13
  • check the sequence of events after move there this modify trigger – Yogesh Commented Sep 9, 2014 at 14:36
Add a ment  | 

1 Answer 1

Reset to default 9

I'm a little late, but just in case anyone else has this problem - you just need to move your mon 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
});

本文标签: javascriptfabricjs assign event listener to an object for multiple eventsStack Overflow