jashkenas / backbone

Give your JS App some Backbone with Models, Views, Collections, and Events
http://backbonejs.org
MIT License
28.09k stars 5.38k forks source link

fetching local files #4166

Closed sadikyalcin closed 6 years ago

sadikyalcin commented 7 years ago

I'm developing a nw.js app. I have en existing slider built via backbone which renders a json file (I'm not familiar with backbone at all).

I'm trying to ".fetch" a file stored on the file system of a device - no servers involved. I can reach the file with node modules but don't know how to pass that data to .fetch function or fetch the file with ://file protocol.

There are no security issues as I can launch the app with the security disabled.

Package json for nw.js;

"chromium-args": "--disable-web-security --allow-file-access-from-files --allow-file-access --user-data-dir --enable-local-file-accesses",

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,
  });
},

This doesn't work. Error callback is trigged. I can see the JSON in the response text but status is '0'.

I'm not sure if this is an issue with backbone or the chromium args aren't working with nw.js but I could do the following and pass that JSON for backbone to render but don't know how.

var fs = require('fs');
fs.readFile(url, 'utf8', function (err, data) {
  if (err) throw err;
  var book = JSON.parse(data);
  // render the json view backbone view
});
sadikyalcin commented 7 years ago

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;
jashkenas commented 6 years ago

I don't think this really has anything to do with Backbone. Good luck!