Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  192] [ 5]  / answers: 1 / hits: 12920  / 11 Years ago, mon, january 27, 2014, 12:00:00

I have an ajax function on the initialize of the main router that seems to hinder the event of my signin button in signin.js. When I click the signin button, it doesn't perform its function, instead the browser places the inputs on the URL, (i.e. username and password).
But when I remove the ajax function on the initialize, I can successfully log in.



I've included some of the codes I'm working on. Thanks



main.js



initialize: function(){
$.ajax({
type: GET,
url: something here,
contentType: application/json,
headers: {
'someVar': something here
},
statusCode: {
404: function() {
console.log('404: logged out');
if (!this.loginView) {
this.loginView = new LoginView();
}
$('.pagewrap').html(this.loginView.el);
},
200: function() {
console.log('200');
if (!this.homeView) {
this.homeView = new HomeView();
}
$('.pagewrap').html(this.homeView.el);
}
}
});
// return false;
},


signin.js



var SigninView = Backbone.View.extend ({
el: '#signin-container',
events: {
click #btn-signin : submit
},
submit: function () {
console.log('signin');
$.ajax({ ... });
return false;
}
});
var toSignin = new SigninView();
window.anotherSigninView = Backbone.View.extend({
initialize: function() {},
render: function() {}
});


home.js



window.HomeView = Backbone.View.extend ({
initialize: function() {
this.render();
},
render: function() {
$(this.el).html( this.template() );
return this;
}
});


some html



<form id=signin-container>
<table id=tbl-signin>
<tr>
<td><div class=input-box><input class=input-text type=text name=username placeholder=Username></div></td>
<td><div class=input-box><input class=input-text type=password name=password placeholder=Password></div></td>
<td><input id=btn-signin class=button value=Sign In></td>
</tr>
<tr>
<td class=opt><input class=checkbox type=checkbox name=rememberMe value=true><label class=opt-signin>Remember Me?</label></td>
<td class=opt><a class=opt-signin href=#>Forgot Password?</a></td>
<td></td>
</tr>
</table>
</form>

More From » jquery

 Answers
6

Ok, I figured out what's your problem :)



Here is an example that resumes your code jsfiddle.net/26xf4/6. The problem is that you don't call new SigninView(); (instantiate the view) hence its events are never bound.



So, in this example try to uncomment the ligne 43 and your code will work as expected, because when you instantiate a View (new SigninView()) its constructor calls the delegateEvents() function (you don't see this in your code, it's in the backbone.js) enabling the events you declare in your view :



events: {
click #btn-signin : submit
},

[#48323] Saturday, January 25, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
analiseb

Total Points: 252
Total Questions: 96
Total Answers: 106

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
analiseb questions
;