I need to represent a complex model in MVC (model view collection) architecture. I’m using backbone.js with jsTree.
I’m retrieving data from the server as JSON and making a tree with it. For displaying the tree I’m using jQuery’s jsTree plugin. The data represents file system architecture of some remote system.
The problems I’m encountering are these:
- Should I create two models? One representing directory and one representing file because there can be more files inside the directory.
- How can I attach the view with the UI? I want to listen to all the events and do some server side action depending upon them because the view is being dynamically made by the jsTree plugin.
- If I make 2 models do I need to make 2 collections? Would a singe one would do because ids in the full set of all the models will be unique?
Thanks
Directory and file really represent the composite design pattern. You could have two models (
FileandDirectory) but then use polymorphism to do something likethis.getContents().I don’t have much experience with backbone, so can’t advise on point 2 or 3. (I suggest that you split these into other questions.)