arturadib / agility

Javascript MVC for the "write less, do more" programmer
http://agilityjs.com
MIT License
542 stars 70 forks source link

submit event for forms does not fire #64

Closed docyes closed 12 years ago

docyes commented 12 years ago

I can't seem to setup an event listener for the submit event on a form. See example below...

var searchbar = $$({ model:{}, view:{ format: '

\ \ \
', style: '& input[type="search"] {width:70%;} \ & input[type="submit"] {width: 10%;}' }, controller:{ 'submit form': function(){ alert(16); } } }); $$.document.append(searchbar);

arturadib commented 12 years ago

At the moment Agility won't include the root element of a view in the event selector (this behavior can be traced back to jQuery), so the controller submit form doesn't pick up any element.

To get around it, you can either explicitly select the root element with submit & (see http://agilityjs.com/docs/docs.html#events-dom), or create a dummy root element for your view so that the form selector is picked up by jQuery:

var searchbar = $$({
   model:{},
   view:{
       format:
           '<div>\
             <form> \
               <input type="search"> \
               <input type="submit" value="search"> \
             </form> \
           </div>',
       style:
           '& input[type="search"] {width:70%;} \
            & input[type="submit"] {width: 10%;}'
   },
   controller:{
       'submit form': function(){
           alert(16);
       }
   }
});
$$.document.append(searchbar);
docyes commented 12 years ago

Thanks, totally reasonable!