I am experiencing a strange behavior with backbone view, On my view template, the image tag flickers and then disappears immediately when the view is rendered, no image is then displayed this is my first backbonejs/nodejs application and I have spent quite some time trying to debug this,I hope I am clear enough, thanks.
Here is my code:
//The Backbone view
define(['text!templates/profile.html'],function(profileTemplate){
var profileView=Backbone.View.extend({
el:$('#content'),
initialize:function(){
this.model.bind('change',this.render,this);
},
viewTemplate: _.template(profileTemplate),
render:function(){
this.model.fetch();
this.$el.html(this.viewTemplate(this.model.toJSON()));
}
});
return profileView;
});
//HTML TEMPLATE (profile.html)
<img src="uploads/<%= photo%>" alt="image" />
//SCHEMA
var AccountSchema=new mongoose.Schema({
email:{type:String,unique:true},
password:{type:String},
name:{
first:{type:String},
last:{type:String},
},
photo:{type:String},
});
Directory Structure
/ParentDirectory
/Public
/templates
profile.html //this is the template being rendered
/uploads //contains images
//ROUTER
define(['views/profile'],function(ProfileView){
var router=Backbone.Router.extend({
currentView:null,
routes:{
"profile/:id":"profile"
},
//Calls render method on views
changeView:function(view){
if(null !=this.currentView){
this.currentView.undelegateEvents();
}
this.currentView=view;
this.currentView.render();
},
profile:function(id){
var model=new Account({id:id});
this.changeView(new ProfileView({model:model}));
},
return new router();
});
There is some issue here on the code:
initialize:function(){
this.model.bind('change',this.render,this);
},
render:function(){
this.model.fetch();
this.$el.html(this.viewTemplate(this.model.toJSON()));
}
You are fetching the model inside render function (which just feels wrong) the model get updated and trigger a change event that calls render which starts the same loop all over again.