放弃,不知道为什么Backbone没有取出我的json。 我的检查员说:"未捕获的ReferenceError:业务未定义" 有人可以帮帮我吗?
这是我的代码:
//-------------
// Model:
//-------------
var DirectoryItem = Backbone.Model.extend();
//-------------
// Collection:
//-------------
var Directory = Backbone.Collection.extend({
model: DirectoryItem,
url: 'JSON/directory.json',
parse: function (data) {
return data.Businesses
}
});
//-------------
// List View:
//-------------
var DirectoryListView = Backbone.View.extend({
el: $("#directoryView"),
events: {
"click li": "itemClicked"
},
initialize: function() {
this.collection = new Directory();
this.collection.fetch();
this.render();
this.bind('change', this.render, this);
},
render: function () {
var business = new Directory();
var that = this;
business.fetch({
success: function (Directory) {
var template = _.template($('#item-list-template').html(), {Directory: Directory.models});
that.$el.append(template);
}
});
that.$el.toggleClass('view');
return that;
},
itemClicked: function(e){
//we get the id of the clicked item throught his data property from the html
var id = $(e.currentTarget).data('id');
//we obtain the right model from the connection with the id
var name = this.collection.get(id);
//we load the view of the selected model
var directoryItemView = new DirectoryItemView({ model: name });
}
});
这是我的模板:
<script type="text/template" id="item-list-template">
<li><%= Business %></li>
</script>
以下是我的JSON示例:
{
"Businesses":[
{
"Business" : "Busines nº1",
"Field" : "Marketing - web dev agency",
"Contact Info" : "info@arcstone.com",
"Link" : "http://www.web.com/contact-us",
"Where?" : "Downtown",
"Round:" : 0,
"follow up" : "",
"Comments" : ""
},
{
"Business" : "Busines nº2",
"Field" : "University",
"Contact Info" : "",
"Link" : "https://www.web.edu",
"Where?" : "",
"Round:" : 0,
"follow up" : "",
"Comments" : ""
},...
非常感谢!
答案 0 :(得分:2)
您传递给下划线的数据({Directory: Directory.models}
)没有Business
属性,因此出错。
您传递的是一系列模型,您应该迭代它并从每个模型访问Business
<% _.each(Directory, function(model) { %>
< li > <%= model.get("Business") %> < /li>
<% }); %>