图像标签闪烁然后消失 - backbonejs

时间:2013-06-30 23:46:16

标签: node.js backbone.js

我遇到了骨干视图的奇怪行为,  在我的视图模板上,图像标记闪烁,然后在渲染视图时立即消失,然后不显示任何图像  这是我的第一个backbonejs / nodejs应用程序,我花了很多时间尝试调试它,我希望我很清楚,谢谢。

这是我的代码:

// Backbone视图

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},
  });

目录结构

/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();
    });

1 个答案:

答案 0 :(得分:0)

代码中存在一些问题:

     initialize:function(){
      this.model.bind('change',this.render,this);
    },

    render:function(){
        this.model.fetch();
        this.$el.html(this.viewTemplate(this.model.toJSON()));
    }

您正在获取渲染函数中的模型(这只是感觉不对)模型得到更新并触发一个调用渲染的更改事件,它会重新开始相同的循环。