我遇到了主干视图的奇怪行为,在我的视图模板上,图像标签闪烁,然后在呈现视图时立即消失,然后没有图像显示这是我的第一个骨干js / nodejs应用程序,我花了很长时间尝试调试这个,我希望我足够清楚,谢谢。
这是我的代码:
//主干视图
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 模板 (profile.html)
<img src="uploads/<%= photo%>" alt="image" />
//架构
var AccountSchema=new mongoose.Schema({
email:{type:String,unique:true},
password:{type:String},
name:{
first:{type:String},
last:{type:String},
},
photo:{type:String},
});
目录结构
/ParentDirectory
/Public
/templates
profile.html //this is the template being rendered
/uploads //contains images
//路由器
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();
});