質問

私は以下のような回答リストを持っています:

enter image description here

各リスト項目はバックボーンモデルです。

{
  title: 'answer title...',
  content: 'answer content...',
  voteStatus: 'up'
}

アップ投票またはダウン投票をクリックすると、モデルの voteStatus 変更され、この回答項目が再レンダリングされます。

回答のコンテンツに画像がある場合、画像も再レンダリングされますが、これは私が望むものではありません。

変更したときに投票ボタンを再レンダリングするにはどうすればよいですか voteStatus?

役に立ちましたか?

解決

あなたの中にサブビューを持っている 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;
  }
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top