Ember Data and file:/// urls (specifically within NodeWebkit)


#1

I’m building an Ember app within the NodeWebkit framework. When I started it I decided to use plain Ember.Object for my Models:

App.Person = Ember.Object.extend({
    First: null,
    Last:  null
});

Then I have a loadData method which reads a JSON file using Node, parses it, and returns the data to the page for Ember to iterate over it.

Now I’d like to start using Ember Data. When I implement the model hook in my route:

App.PeopleRoute = Ember.Route.extend({
    name: 'people',
    model: function(){
        // debugger;
        return this.store.find('person');
    }
});

The AJAX call fails because it’s pointing at file:///people instead of a legitimate resource. I know that I can specify a URL in my Adapter, which currently looks like this:

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: 'api/1'
});

But that merely pushes the issue down the chain. Is there any way I can reference a file URL relative to my application in which Ember Data could access this JSON file?

I should say that the current approach I’m using works perfectly. The main reason I’m wanting to move to Ember Data is so that I can experiment with the new testing methods.

Would love some advice and input.


#2

Looks like this SO post might do the trick:

Specifically this bit:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  // ...

  suffix: '.json',

  pathForType: function(type) {
    this._super(type) + this.get('suffix');
  }
});

In my case it looks like this:

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: '',
    suffix: '.json',
    pathForType: function(type) {
        // explicitly read from the file system
        var root = nwGui.App.dataPath,
            path = '/%@/%@%@'.fmt(root, this._super(type), this.get('suffix'));
        return path;
    }
});

NodeWebkit has a built in property which evaluates to the directory where the application code lives. So the above pathForType method would evaluate to:

//Users/amatthews/Library/Application Support/directory/people.json


#3

Are you using the app protocol?https://github.com/rogerwang/node-webkit/wiki/App-protocol


#4

I am not. I didn’t even know that was an option. Hasn’t been in any of the articles/tutorials I’ve read. Thanks Ben!