2

I am trying out different functionalities using backbone and i came accross a strange one. I am trying to submit a form through backbone. I had done this previously and i cannot find whats wrong with what i am doing.

The code is as follows :

HTML Part

   <div clas="loginpage"></div>
   <form class="login-user-form">
     <input type="text" name="name" id="name" placeholder="Enter Name"><br><br>
     <button type="submit" class="btn">Create</button>
   </form>

jQuery Part

var UserLogin = Backbone.View.extend({
   el:'.loginpage',
   initialize:function(){
   console.log("Login View Initialized");
 },
 events:{
   'submit .btn' : 'loginuser'
 },
 loginuser:function(){
   console.log("Login Clicked.");
   return false;
 }
});
var userlogin = new UserLogin();

I get Login View Initialized message in console. But i cannot get the loginuser function to work. The page submits through its default submit functionality.

What am i doing wrong?

Roy M J
  • 6,926
  • 7
  • 51
  • 78

2 Answers2

4

1) loginpage doesn't contain the form. Fix:

<div class="loginpage">
  <form class="login-user-form">
    <input type="text" name="name" id="name" placeholder="Enter Name"><br><br>
    <button type="submit" class="btn">Create</button>
  </form>
</div>

2)

events : {
 'submit' : 'loginuser'
},

loginuser : function(){
  console.log("Login Clicked.");
  return false; // Stops default html form submission
}
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55
  • If i remove the return false, the basic html form submission take place. This i wanted to handle through jquery itself and hence added the return false.:).. – Roy M J Jul 04 '13 at 09:52
0

Got it working :

events:{   
    'submit' : 'loginuser'
}

Got this from the following thread : How do I get backbone to bind the submit event to a form?

Cheers ..:)

Community
  • 1
  • 1
Roy M J
  • 6,926
  • 7
  • 51
  • 78