I'm new to backbone, i cant understand why this events doesnt fire. I tried few different ways but it doesnt work in each way. The screen is rendering. Im testing on a mobile with the lastest cordova.
login.js
define(["jquery", "underscore", "kinvey", "handlebars", "text", "text!templates/Login.html"],
function ($, _, Kinvey, Handlebars, Text , source) {
var Login = Backbone.View.extend({
events: {
"touchend #submit": "loggati",
"touchend #signup": "goToSignup"
},
initialize: function () {
console.log("init");
/* won't go this way either
$('#submit').click(function(){
alert('ciao');
});
$('#signup').click(function(){
alert('ciao');
*/
});
},
render: function (eventName) {
console.log("rendering");
var template = Handlebars.compile(source);
var context = {color : "blue"};
var html=template(context);
$(document.body).html(html);
return this;
},
goToSignup: function () {
console.log("signup : fired");
//stuff
},
loggati: function() {
console.log("loggati : fired");
//stuff
}
});
return Login;
});
Login.html
<div data-role="header" >
<h1>Login</h1>
</div>
<div data-role="content">
<div data-role="fieldcontain">
<input id= "username" placeholder="Username" value="" type="text">
</div>
<div data-role="fieldcontain">
<input id= "password" placeholder="Password" value="" type="password">
</div>
<a data-role="button" id="submit">Sign In</a>
<a data-role="button" id="signup">Register</a>
</div>
router.js
define(["jquery", "underscore", "kinvey", "views/Login", "views/GetToken", "views/Game", "views/SignupPage", "views/Home", "views/Settings", "views/Rules"],
function ($, _, Kinvey, Login, GetToken, Game, SignupPage, Home, Settings, Rules) {
var Router= Backbone.Router.extend({
routes: {
"login": "login",
"getToken": "getToken",
"game": "game",
"signupPage": "signupPage",
"home": "home",
"settings": "settings",
"rules": "rules"
},
initialize: function () {
console.log('initialize');
this.currentView = undefined;
var page;
if( window.localStorage.getItem("username") && window.localStorage.getItem("username").length > 0)
{
console.log('trovato utente loggato');
document.getElementById("nomeSet").innerHTML = window.localStorage.getItem("username");
document.getElementById("tokenSet").innersHTML = window.localStorage.getItem("token");
page = new Home();
}
else
page = new Login();
this.changePage(page);
},
login: function () {
var page = new Login();
this.changePage(page);
},
getToken: function () {
var page = new GetToken({
});
this.changePage(page);
},
game: function () {
var page = new Game({
});
this.changePage(page);
},
signupPage: function () {
var page = new SignupPage({
});
this.changePage(page);
},
home: function () {
var page = new Home({
});
this.changePage(page);
},
settings: function () {
var page = new Settings({
});
this.changePage(page);
},
rules: function () {
var page = new Rules({
});
this.changePage(page);
},
changePage: function (page) {
console.log('changepage '+page);
if(this.currentView) {
this.currentView.remove();
this.currentView.off();
}
this.currentView = page;
page.render();
}
});
return Router;
});
A Backbone view's events are attached to its el
using event delegation. Every view has an el
, if you don't specify one yourself then your view will use an empty <div>
as its el
. You're not specify an el
anywhere so your view will create a <div>
for as the el
for you. A render
method usually looks like this:
render: function() {
var html = html_from_some_template;
this.$el.html(html);
return this;
}
but you're saying:
$(document.body).html(html);
to add the HTML. Your view's el
is not body
so none of your events work.
A quick fix is to add:
el: 'body'
to your view definition and then change your render
to say:
this.$el.html(html);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.