简体   繁体   English

Backbone.js - View应该如何更新多个HTML元素?

[英]Backbone.js - How should a View update multiple HTML elements?

Given the following HTML 给出以下HTML

<div id="outer">
  <div id="innerA"></div>
  <div id="innerB"></div>
  <div id="innerC"></div>
</div>

Should a View's render method update each element individually, ignoring the parent (outer) div: 如果View的render方法单独更新每个元素,则忽略父(外)div:

MyView = Backbone.View.extend({
  model: null,

  initialize: function(options) {
    var self = this;
    this.model = options.model;
    _.bindAll(this, 'render');
    this.model.bind('change', this.render);
  },

  render: function() {
    var self = this;
    $('#innerA').html(this.model.get('A'));
    $('#innerB').html(this.model.get('B'));
    $('#innerC').html(this.model.get('C'));
  },
});

Or should it write HTML and values: 或者它应该写HTML和值:

MyView = Backbone.View.extend({
  el: '#outer',
  model: null,

  initialize: function(options) {
    var self = this;
    this.model = options.model;
    _.bindAll(this, 'render');
    this.model.bind('change', this.render);
  },

  render: function() {
    var self = this;
    var element = jQuery(this.el);
    element.html('');
    element.append('<div id="innerA">' + this.model.get('A') + '</div>');
    element.append('<div id="innerB">' + this.model.get('B') + '</div>');
    element.append('<div id="innerC">' + this.model.get('C') + '</div>');
    return this.el;
  },
});

Or should it create nested models and views to delegate rendering: 或者它应该创建嵌套模型和视图来委托渲染:

MyView = Backbone.View.extend({
  model: null,

  initialize: function(options) {
    var self = this;
    this.model = options.model;
    _.bindAll(this, 'render');
    this.model.bind('change', this.render);
  },

  render: function() {
    var self = this;

    var modelA = new ModelA();
    modelA.set({
      value: this.model.get('A'),
    });
    var viewA = new ViewA({
      model: modelA
    });

    // Assume ViewA renders '<div id="innerA"></div>'
    $('#innerA').html(viewA.render().html());

    // Rinse and repeat for B and C.
  },
});

This totally depends on the content of #innerA , #innerB , #innerC etc. 这完全取决于#innerA#innerB#innerC等的内容。

If those are independent functionalities with their own collection/model where they update & retrieve data, it's better to keep them as separate view's. 如果这些是具有自己的集合/模型的独立功能,他们更新和检索数据,最好将它们保持为单独的视图。 They'll completely handle their functionalities, where the parent view will simply control actions such as their creation, deletion etc. 他们将完全处理他们的功能,其中父视图将简单地控制诸如创建,删除等操作。

In the case of your first two example, all of these seems to be using the same model and is just printing single attribute. 在前两个示例的情况下,所有这些似乎都使用相同的模型,只是打印单个属性。 There is no need to create separate views. 无需创建单独的视图。 The rendering is commonly handled by using a template engine like underscores template method: 渲染通常通过使用模板引擎(如下划线模板方法)来处理:

MyView = Backbone.View.extend({
  template: _.template($('#temp').text()),
  initialize: function(options) {
    this.listenTo(this.model, 'change', this.render);
    this.render();
  },
  render: function() {
    this.$el.append(this.template(this.model.toJSON()));
  }
});
var view = new MyView ({
 model: new Backbone.Model(),
 el: $('#outer')
});
<div id="outer">
</div>
<script type="text/template" id="temp">
  <div id="innerA"><%=A%></div>
  <div id="innerB"><%=B%></div>
  <div id="innerC"><%=C%></div>
</script>

In case #innerA , #innerB , #innerC where 3 independent functionalities, You'll have 4 views with the parent view creating all the 3 child views and appends to it as part of it's rendering. 如果是#innerA #innerB#innerC #innerA#innerB ,其中有3个独立的功能,你将有4个视图,父视图创建所有3个子视图,并作为其渲染的一部分附加到它。


Side notes: 附注:

  • Backbone will automatically set model & collection options to the view if present, no need to do this manually 如果存在,Backbone将自动为视图设置modelcollection选项,无需手动执行此操作
  • We generally don't use global selectors like $('#innerA') within a view ( ignore the template selector for demo purpose ). 我们通常不在视图中使用全局选择器(如$('#innerA')忽略模板选择器以进行演示 )。 Always limit DOM access within the view like this.$('#innerA') (alias for this.$el.find('#innerA') ) 始终在视图中限制DOM访问,如下this.$('#innerA') (别名为this.$el.find('#innerA')
  • Read this answer regarding the other changes I've made. 阅读这个答案,了解我所做的其他改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM