【发布时间】:2017-02-27 15:01:20
【问题描述】:
我想使用 underscore.js 获取数组。
这是我的情况。
view.js
views.list = Backbone.View.extend({
render: function(templateName) {
var template = _.template(templateName);
this.$el.html(template({result : this.collection.models}));
_.each(this.collection.models, function(model){
console.log(model.get("id"));
});
return this;
}
});
运行结果_.each(this.collection.models, function(model){console.log(model.get("id"));});
list.html
<div id="columns">
<% _.each(result, function(model){ %>
<div id="<% model.get("id") %>" class="content">
<a href="<% model.get("url") %>">
<figure>
<img src="<% model.get("imgSrc") %>">
<figcaption><% model.get("title") %></figcaption>
</figure>
</div>
<% }); %>
</div>
我给this.collection.model发了一个参数作为result参数,所以我觉得上面的可执行代码和我在html中写的可执行代码是一样的,但是运行结果不一样。
有什么区别?
【问题讨论】:
-
不是每次调用render时都执行
var template = _.template(templateName);,而是将模板函数存储为视图的template属性 -
@TJ 好的,谢谢 :) 我今天又认识了一个
标签: javascript backbone.js underscore.js underscore.js-templating backbone.js-collections