Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Backbone model design
    primarykey
    data
    text
    <p>Fairly new to JavaScript so it might be a noobish question.</p> <p>At the moment for my project I'm using NodeJS for my server and Backbone for the client. The client will send a request to the server and the server will send list of files in the server, my aim was to simply return the list of files and when user click on the file it will send another request to the server to load the file.</p> <p>Currently in the client level my model and collection is defined something like:</p> <pre><code>app.MyFile = Backbone.Model.extend({ defaults: { modifiedDate: new Date(), path: '', content: '' // content of the file } }); var MyFileList = Backbone.Collection.extend({ model: app.MyFile, url: '/api/files' }); // create global collection of files app.MyFiles = new MyFileList(); 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(); }); </code></pre> <p>And my server code:</p> <pre><code>var application_root = __dirname, express = require("express"), ... app.get('/api/files', function(req, res) { ... // return file list } app.get('/api/files/:id', function(req, res) { ... // return file content? } </code></pre> <p>Since it doesn't make sense to load all files in the directory and send it back to the client, what I did was I created the model in the server and fill up <code>modifiedDate</code> and <code>path</code> while leaving <code>content</code> to <code>null</code>. But the problem now is that how do I fill up the <code>content</code> when user clicks on the file? I'm not sure how to manually send an HTTP request from Backbone View or controller. Or is there any better way of doing this? One way that I can think of is to create another model that only keeps <code>modifiedDate</code> and <code>path</code> but to me this looks very verbose and repetitive.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload