I would like to build my first view inheritance on Backbone.js, very classic, a template which is a navbar with the register/login/logout options as a father of all my other views.
The code seems to works find, each initialize method are called, but it doesn't work at the render part.
I can instanciate templateFrontendView() (the fathter) which works fine (i have the navbar on my view), or i can instanciate homeView() (child of templateFrontendView) but i'll just have the render of this one, the render of templateFrontendView disapears even i call it in the initialize method...
Here is the jsfiddle with all the code
window.User = Backbone.Model.extend({
defaults : {
id : "1",
username : "anonymous",
facebook_id : "1235486421",
facebook_token : "AEZH93FZFEFSFSFS4545154sfsfSF"
},
urlRoot: 'http://localhost:3000/user',
initialize : function() {
console.log('User Constructor...');
this.url = "http://localhost:3000/user/"+this.get('username')+'/'+this.get('facebook_id')+'/'+this.get('facebook_token');
}
});
window.templateFrontendView = Backbone.View.extend({
el : $('#template-frontend-container'),
initialize : function() {
this.model = new User();
this.template = _.template($('#template-frontend-template').html());
this.render();
},
events: {
"click a.fb_connect": 'fetch_user'
},
fetch_user: function(){
console.log("Save requested...");
},render : function() {
this.delegateEvents();
var renderedContent = this.template(this.model.toJSON());
$(this.el).html(renderedContent);
return this;
}
});
window.homeView = templateFrontendView.extend({
el : $('#home-container'),
initialize : function() {
templateFrontendView.prototype.initialize.apply(this);
this.model = new User();
this.template = _.template($('#home-template').html());
this.render();
},
render : function() {
this.delegateEvents();
var renderedContent = this.template(this.model.toJSON());
$(this.el).html(renderedContent);
return this;
}
});
var App = {
home: function() {
var V = new homeView();
}
}
$(document).ready(function(){
App.home();
});
And the view
<!-- Template Frontend -->
<script type="text/template" id="template-frontend-template">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Chon the Road</a>
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a style="display: none;" class="account" href="#">Welcome <span><%= username %></span></a></li>
<li><a style="display: none;" class="fb_connect" href="#"><img src="img/fb-connect.png" alt="fb-connect" /></a></li>
</ul>
</div>
</div>
</script>
<div id='template-frontend-container'></div>
<!-- Template Frontend -->
<div id="fb-root"></div>
<!-- Home -->
<script type="text/template" id="home-template">
<span><%= username %></span>
</script>
<div id='home-container'></div>
<!-- Home -->
Thanks !