I'm trying to develop a simple file browser using Backbone for the client and Node.js for the server. At the moment I'm quite stuck on how to design the model when it comes to subfolder
Client code looks something like
app.MyFile = Backbone.Model.extend({
defaults: {
path: '', // id of the model will be the path + filename
content: '', // content of the file
isDir: false // if file is a directory
}
});
var MyFileList = Backbone.Collection.extend({
model: app.MyFile,
url: '/api/files'
});
// create global collection of files
app.MyFiles = new MyFileList();
// displays file name
var MyFileItemView = Backbone.View.extend({
events: {
'click .clickable': 'view'
},
...
view: function (source) {
if (this.model.toJSON().isDir) {
this.model.fetch();
// XXX what to do here
_.each(this.model.get('content'), function (obj) {
console.log(obj.toJSON()); // error - no toJSON method
});
} else {
// calls another view that calls the model.fetch
// to display the content (no issue here)
}
},
});
var MyFilesListView = Backbone.View.extend({
initialize: function () {
// XXX Not sure if I should listen on MyFiles collection...
app.MyFiles.on('reset', this.render, this);
},
render: function () {
app.MyFiles.each(function (file) {
new MyFileItemView({model:file});
}, this);
});
app.AppView = Backbone.View.extend({
initialize: function () {
// fetch all files
app.MyFileList.fetch();
}
});
// app.js (point of entry)
$(function() {
// Kick things off by creating the **App**.
new app.AppView();
});
My server code:
var express = require("express"),
...
app.get('/api/files', function(req, res) {
...
// return file list (including folder - no issue here)
}
app.get('/api/files/:id', function(req, res) {
var fileModel = createFileModel(req.params.id); // create file model
if (!fileModel.isDir) {
// if file is not directory, then simply read the content and return
// it back to the client -- no issue
...
} else {
var files = [];
// read the directory to find all the files and push it to
// files array
...
fileModel.content = files;
res.send(fileModel);
}
}
At the moment I'm not sure what is the correct way to do this. My questions:
- How to represent the model object itself. Should I just set the content to collection of MyFile if
isDir===true
? If this is the case, how should I do that? callingtoJSON()
on the model's content throws an exception becausetoJSON
is not defined - Also on the
MyFilesListView
should it listen to the global collection as well? Now that I need to handle sub folders it doesn't seem right. - Or should I overrides the global collection when trying to view the subfolder?
- Is my server code implementation correct? The problem I'm having now is that when I put the content into array and send fileModel back to the client, the list of files are not reflected in the model itself - perhaps I must override
parse
?
I read some posts here
- Nested models of the same 'class' in the Backbone.js framework?
- Backbone: fetch collection from server
- How to build a Collection/Model from nested JSON with Backbone.js
But I'm still not sure how to apply it... Javascript is confusing :(