Backbone parsing json response

前端 未结 2 778
走了就别回头了
走了就别回头了 2021-02-09 20:58

I\'ve been using Backbone for a total of 3 days now, and I can see that this has been asked about quite a lot, but honestly I\'m just not getting it. I\'ve been banging my head

相关标签:
2条回答
  • 2021-02-09 21:35

    You should use the parse() method in your collection :

    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;  
       }    
    
    });
    

    There is one parse() in the MOdel as well as in the Collection, for the same purpose on url() handling.

    EDIT : I'm not an expert in the Router, but I suppose you have to render the View at some point.

    var view = new Contact.Views.Employees({ collection: Contact.employees }); 
    view.render();
    
    0 讨论(0)
  • 2021-02-09 21:42

    model comes with parse method , so when ever you are making mode.fetch() request , the output can be parse in model itself

    example :

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

    var Model = new model({})

    0 讨论(0)
提交回复
热议问题