如何在骨干中重新渲染部分模型?

Rob*_*bin 8 templates model rerender backbone.js

我有一个如下的答案列表:

在此输入图像描述

每个列表项都是骨干模型.

{
  title: 'answer title...',
  content: 'answer content...',
  voteStatus: 'up'
}
Run Code Online (Sandbox Code Playgroud)

当我点击向上投票或向下投票时,模型voteStatus将会更改,并且此答案项目将重新呈现.

如果答案内容中有图片,图片也会重新渲染,但这不是我想要的.

当我改变时,我怎么能重新渲染投票按钮voteStatus

idb*_*old 7

在你的内部有一个子视图AnswerView,只负责渲染投票箭头,VotingArrowsView.你会初始化这里面子视图initialize的功能AnswerView,然后在前面加上子视图的el父视图的el时候render荷兰国际集团父视图:

var AnswerView = Backbone.View.extend({
  initialize: function(options){
    this.template = _.template($('#answerTmpl').html());
    this.votingArrowsView = new VotingArrowsView({ model: this.model });
    ...
  },
  render: function(){
    this.$el.html(this.template(this.model.toJSON()));
    this.$el.prepend(this.votingArrowsView.render().el);
    return this;
  },
  ...
});

var VotingArrowsView = Backbone.View.extend({
  initialize: function(options){
    this.template = _.template($('#votingArrowsTmpl').html());
    this.listenTo(this.model, 'change:voteStatus', this.render);
  },
  render: function(){
    this.$el.html(this.template(this.model.toJSON()));
    return this;
  }
});
Run Code Online (Sandbox Code Playgroud)