我正在尝试获取一些数据并使用 Backbone 显示它,但没有运气 - 我没有收到任何错误,只是没有渲染(我使用 todos 示例作为指南构建了这个非常简单的应用程序)
索引.html:
<!doctype html>
<html>
<head>
<title>Hello Backbone</title>
</head>
<body>
<div id="sps-container">
<div id="main">
<ul id="service-providers"></ul>
</div>
</div>
</body>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<script type="text/javascript" src="/app.js"></script>
<!-- Templates -->
<script type="text/template" id="sp-template">
<div class="view">
<label><%- name %></label>
</div>
</script>
</html>
应用程序.js
$(function(){
var ServiceProvider = Backbone.Model.extend({
defaults : function() {
return {
id : "",
version : 1,
name : ""
};
}
});
var ServiceProviderList = Backbone.Collection.extend({
url: '/web-admin/faces/Backbone/data.json',
model : ServiceProvider
});
var myCollection = new ServiceProviderList();
var ServiceProviderView = Backbone.View.extend({
el : $("#service-providers"),
tagName : "li",
initialize : function() {
_.bindAll(this, 'render');
this.template = _.template($('#sp-template').html());
this.render();
},
render : function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
myCollection.fetch(function(c) {
var v = new ServiceProviderView(c);
main.html(v.render().el);
});
var SPView = new ServiceProviderView();
});
数据.json
[
{
"id": "03c88b31-6719-4662-9ad8-64f247de13f0",
"version": 1,
"name": "Dino Tel"
},
{
"id": "936690b4-6a4a-436d-abed-8ca316fbae22",
"version": 1,
"name": "Krusty Tel"
}
]
我认为它没有加载任何数据 - 虽然没有其他错误,并且 json 文件可用。我错过了什么吗?