Search code examples
javascriptnode.jsbackbone.jsnwjs

Load model from JSON on backbone


I'm not familiar with backbone.js. Currently developing a nw.js app where I have to embed an existing app that was built via backbone.

I do not want to run a local webserver just to use .fetch method if there are alternatives. I can easily read the files with node fs package.

Here is the code;

loadpages: function(){
  this.activeBook.fetch({
    url: url, //file://.......
    success: function(collection, response, options){
      self.renderBook();
      self.renderNav();
    },
    error: function(collection, response, options){
      //alert("Error");
      return;
    }
  });
}

renderBook: function(){
  myapp.bookView = new myapp.BookView({
    collection: this.activeBook,
    instanceURL : url,
  });
},

Target:

var fs = require('fs');
fs.readFile(url, 'utf8', function (err, data) {
  if (err) throw err;
  var book = JSON.parse(data);
  // render the json as model
});

Note: I do have --allow-file-access-from-files --allow-file-access chromium args but fetch still doesn't work.


Solution

  • OK, maybe not a good idea since I'm patching jQuery but I've managed to get around this by changing jQuery's .done callback to add status code '0' to success callback;

    // Determine if successful
    
    isSuccess = status === 0 || status >= 200 && status < 300 || status === 304;