【问题标题】:Binding a model to a backbone client template将模型绑定到骨干客户端模板
【发布时间】:2011-09-26 06:19:45
【问题描述】:

我有以下backbone.js 客户端模板:

<script id="calleeTemplate" type="text/x-jquery-tmpl">
<tr style="background-color: ${StatusColour}">
    <td class="responder">${ContactFullName}</td>
    <td class="status" style="width:200px">${Status}</td>
    <td class="replied">${Replied}</td>
    <td class="wauto">${Response}</td>
</tr>
</script>

为了能够绑定到这些属性,我有以下视图的渲染方法:

App.Views.Callees = Backbone.View.extend({
  initialize: function () {

  },
  el: $('#calleesStatuses'),
  render: function () {
    var col = _.map(this.collection.models, function (item) {
        return item.attributes;
    });

    $('#calleesStatuses').html('');
    $('#calleeTemplate').tmpl(col).appendTo(this.el);
  }
});

我必须使用下划线_.map 函数从模型中提取属性。我认为这是因为主干使用.get("property") 来提取属性值。

这对我来说似乎不对,我做错了什么吗?

【问题讨论】:

    标签: backbone.js


    【解决方案1】:

    您是对的,您必须转换数据才能轻松地与 tmpl 一起使用。

    但是,最好使用toJSON 而不是直接访问属性。最好也避免直接调用.models

    无论如何您都不需要,主干集合具有一整套 underscore.js 枚举器。因此,您可以将转换缩减为一行:

    var col = this.collection.invoke('toJSON')
    

    【讨论】:

      猜你喜欢
      • 2012-04-03
      • 1970-01-01
      • 2019-09-03
      • 2013-10-14
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多