我已经使用 Backbone 总共 3 天了,我可以看到这已经被问了很多,但老实说我就是不明白。我一直在努力让一个基本的应用程序运行解析嵌套的 json,但我似乎无法解决它。如果我展平 json 响应并删除嵌套元素,这一切都有效,但这不是我接收它的方式。
我已经尝试了一些有关 Backbone 关系的示例,但我真的被困在这里,一个总骨干 n00b,并希望得到一些帮助。
这是HTML:
<div id="employee-data">
<script type="text/template" id="employees-template">
<ol id="data-block">
</ol>
<div class="clear"></div>
</script>
<script type="text/template" id="employee-template">
<h2>Your employer: <span><%= employerName %></span> </h2>
<div>Employee Id: <span><%= employeeId %> </span></div>
<div>Name: <span><%= employeeName %> </span></div>
<div>Title: <span><%= employeeJobTitle %> </span></div>
<div>Location: <span><%= employeeLocation %> </span></div>
</script>
</div>
这是js:
var Contact = {
Models: {},
Collections: {},
Views: {},
Templates:{}
}
Contact.Models.Employee = Backbone.RelationalModel.extend({});
Contact.Collections.Employees = Backbone.Collection.extend({
model: Contact.Models.Employee,
url: "includes/test-data.json",
initialize: function(){
console.log("Employees initialize");
}
});
Contact.Templates.employees = _.template($("#employees-template").html());
Contact.Views.Employees = Backbone.View.extend({
el: $("#employee-data"),
template: Contact.Templates.employees,
initialize: function () {
this.collection.bind("reset", this.render, this);
this.collection.bind("add", this.addOne, this);
},
render: function () {
console.log("render")
console.log(this.collection.length);
$(this.el).html(this.template());
this.addAll();
},
addAll: function () {
console.log("addAll")
this.collection.each(this.addOne);
},
addOne: function (model) {
console.log("addOne")
view = new Contact.Views.Employee({ model: model });
$("ol", this.el).append(view.render());
}
})
Contact.Templates.employee = _.template($("#employee-template").html());
Contact.Views.Employee = Backbone.View.extend({
tagName: "li",
template: Contact.Templates.employee,
initialize: function () {
this.model.bind('destroy', this.destroyItem, this);
this.model.bind('remove', this.removeItem, this);
},
render: function () {
return $(this.el).append(this.template(this.model.toJSON())) ;
}
})
Contact.Router = Backbone.Router.extend({
routes: {
"": "defaultRoute"
},
defaultRoute: function () {
console.log("defaultRoute");
Contact.employees = new Contact.Collections.Employees();
new Contact.Views.Employees({ collection: Contact.employees }); //Add this line
Contact.employees.fetch({
error:function(response, xhr){
console.log(response);
console.log(xhr)
},
success:function(){
console.log("success");
}
});
console.log(Contact.employees.length)
}
})
var appRouter = new Contact.Router();
Backbone.history.start();
最后是json:
[
{
"contactId" : "345345234",
"employees" : [ {
"employeeId" : "EE-00000001",
"employeeName" : "BubbA Ho-tep",
"employeeLegalFirstName" : "Bubba",
"employeePrefFirstName" : "",
"employeeLastName" : "Ho-tep",
"employeeMaritalStatus" : "Single",
"employeeBirthYear" : "1942",
"employeeJobTitle" : "",
"employmentStatus" : "Active",
"employmentTerminationDte" : "",
"employeeReferenceCode" : "EE1",
"employeeDivision" : "HR",
"employeeLocation" : "Downtown",
"employeeEmail" : "bubba.hotep@greatmovies.com",
"employer" : {
"employerId" : "ER-00000001",
"employerName" : "Initech"
}
} ]
}
]