Backbone collections representing tree data

前端 未结 2 379
悲哀的现实
悲哀的现实 2021-02-05 07:15

I want to load json data in form of a tree into Backbone Collection. I can\'t do this. Can anyone explain what I\'m doing wrong?

My very simple model:

CT         


        
2条回答
  •  暖寄归人
    2021-02-05 08:05

    You need to use Backbone.Model parse method which is used to parse data before it gets passed to a model. You can use it to turn data on each level of your tree to Collections and Models representing items on these collections.

    Then when saving you'd have to override the toJSON method on the Model to return the json representation of your data the same way you receive it. It is later on used by Backbone.sync to send the data back to the server. By default it returns only _.clone of your Model.attributes and you want all the Collections and CollectionModels in there as well.

    All hail the backbone! :)

提交回复
热议问题