Is it possible to bind controller function to miltiple events?

35 views
Skip to first unread message

defl...@gmail.com

unread,
May 10, 2013, 7:41:06 AM5/10/13
to agil...@googlegroups.com
Hi. Consider the code:

var list = $$({}, '<div><a href="">click</a></div>', {
  'keyup,dblclick,click a': function(){
    console.log('hello world!');
  }
});

Here i tried to use few different events to bind function to, but this does not work, maybe there is some kind of extra syntax exists to achieve this ?

rjo...@opdots.com

unread,
May 10, 2013, 8:29:20 AM5/10/13
to agil...@googlegroups.com
Hey, in your controller try this instead:

   'keyup a': function(event){ ... },
   'dblclick a': function(event){ ... }, 
   'click a': function(event){ ... },
   // and to trigger on the root div:
   'mouseover &': function(event) {...}

The event handlers must always have an event name, a space, and a selector. Use "&" as the selector for the root element.

Good luck with Agility!

defl...@gmail.com

unread,
May 11, 2013, 3:12:39 AM5/11/13
to agil...@googlegroups.com
Hi Robert, thak you for response, but what if callback function is big enough, should i copy-paste it 4 times ?
Currently i stick with similar soultion:

var list = $$({}, '<div><a href="">click</a></div>', {
  'action': function(){
    console.log('Hello world!');
  },
  'keyup a': function(){ this.trigger('action') },
  'click a': function(){ this.trigger('action') },
  'dblclick a': function(){ this.trigger('action') }
});

But still hope that this could be done more concise way.

пятница, 10 мая 2013 г., 15:41:06 UTC+4 пользователь defl...@gmail.com написал:

defl...@gmail.com

unread,
May 11, 2013, 3:47:53 AM5/11/13
to agil...@googlegroups.com
Browsing test code gave me an anwser, here is correct way to bind to multiple events:

var list = $$({}, '<div><a href="">click</a></div>', {
  'keyup;dblclick;click a': function(){
    console.log('hello world!');
  }
});

Yes, it's just semicolon.

пятница, 10 мая 2013 г., 15:41:06 UTC+4 пользователь defl...@gmail.com написал:
Hi. Consider the code:

Robert Jones

unread,
May 11, 2013, 10:09:55 AM5/11/13
to agil...@googlegroups.com
Hey, I appreciate the semi-colon pointer... nice.

Re copy-paste comment, if the semi-colon syntax weren't there or didn't apply, I would define a named function and reference it like the following. I think it's best practice any way to make code understandable.

var helloWorldEventHandler = function() { 
  console.log( 'hello world!' ); 
};

var list = $$({}, '<div><a href="">click</a></div>', {
  'keyup;dblclick;click a': helloWorldEventHandler
});



Reply all
Reply to author
Forward
0 new messages