Javascript Backbone.js Uncaught ReferenceError到下划线模板
我有一个主干应用程序 我的json看起来像这样Javascript Backbone.js Uncaught ReferenceError到下划线模板,javascript,jquery,json,backbone.js,underscore.js,Javascript,Jquery,Json,Backbone.js,Underscore.js,我有一个主干应用程序 我的json看起来像这样 { "cities": [ "Kävlinge", "Lund", "Enskede", "Vadstena" ] } 我的主干收集代码 var Cities = Backbone.Collection.extend({ url: 'configuration/city.json' }); var Index = Backbone.View.extend({ initialize: functio
{
"cities": [
"Kävlinge",
"Lund",
"Enskede",
"Vadstena"
]
}
我的主干收集代码
var Cities = Backbone.Collection.extend({
url: 'configuration/city.json'
});
var Index = Backbone.View.extend({
initialize: function(){
this.cities = new Cities();
},
el: '.page',
render: function(){
var self = this;
this.cities.fetch({
success: function(cities){
console.log(cities.models[0].attributes.cities);
var template = _.template($('#city-dropdown').html(),{cities: cities.models[0].attributes});
self.$el.html(template());
}
});
}
});
var Router = Backbone.Router.extend({
routes: {
'' : 'home'
}
});
var router = new Router();
var index = new Index();
router.on('route:home',function(){
index.render();
});
Backbone.history.start();
就在下划线模板函数打印所需Json数组之前的console.log语句,但在我的脚本标记中,它只是uncaughtreferenceerror:cities未定义
是我的Json不正确,还是我的主干代码不正确,我很困惑。看起来很傻。请帮忙
<script type="text/template" id="city-dropdown">
<select class="selectpicker">
<%= _.each(cities, alert) %>
</select>
</script>
自下划线1.7.0起: 下划线模板不再接受初始数据对象<代码>\模板现在总是返回函数 因此现在,
\uu0.template
函数的第二个可选参数用于设置模板选项。将对象传递给已编译的函数:
var template = _.template($('#city-dropdown').html());
self.$el.html(template({
cities: cities.models[0].attributes // cities.toJSON()
}));
.template
的行为最近发生了变化(见副本),我怀疑您正在看到这种变化。但直到1.7.0(于2014-08-26发布)之前,您可以说.template(template\u source,data)
直接转到HTML,而无需中间步骤。很多教程和旧代码还没有跟上进度。