6

我已经使用 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"
    }
  } ]
}
]
4

2 回答 2

10

你应该在你的集合中使用parse()方法:

Contact.Collections.Employees = Backbone.Collection.extend({
    model: Contact.Models.Employee,
    url: "includes/test-data.json",

    initialize: function(){
        console.log("Employees initialize");
    },

    parse : function(response){
        return response.employees;  
   }    

});

在模型和集合中都有一个 parse(),用于处理 url() 的相同目的。

编辑: 我不是路由器专家,但我想你必须在某个时候渲染视图。

var view = new Contact.Views.Employees({ collection: Contact.employees }); 
view.render();
于 2012-11-06T19:20:41.057 回答
0

model 带有 parse 方法,所以当你发出 mode.fetch() 请求时,输出可以在模型本身中解析

例子 :

model = Backbone.Model.extend({ urlRoot:"/category/", parse : function(data) { console.log(data); } })

var 模型 = 新模型({})

于 2014-02-13T05:16:23.220 回答