You can write a simple model in a model with a simple function like this in your only model (the xfile model you can name!):
loadFolders : function() { if (this.get('isDir')) { var self = this; self.subFolders = []; _.each(self.get('files'), function(data) { file = new File(data); self.subFolders.push(file); }); } }
and do the same in your view model to display the correct tree view.
And in your express.js backend file (I am not familiar with this framework) you just send the correct json to your base models.
If your data is too long and you want each model directory to be separate (when expanding the user folder in your view), you can create a model with a lot of collections (or collections with a lot of collections) with the same idea, then extract the data from the server in the model, and the backend should support directories, something like this: /api/file/folder1/folder2/file1.txt
Another way to do something more magical is to use a relational model (using any backbone extension that provides such a function)
If you want to know more about:
An example of marionette.js that does the same
Backbone.Relational - relational extension for bakcbone
PS: I canβt provide more links, so you can search for Backbone.associate, which is another relational extension for the backbone
PS 2: you can use coffeescript, which makes javascript syntax simpler and more understandable.
source share