10

I have problems with ember-data. For example, I've created a project at http://localhost/~me/test

In my project I've created a store and a model as follows:

... init stuff here ...

var attr = DS.attr;
App.Person = DS.Model.extend({
    firstName: attr('string'),
    lastName: attr('string'),
});

App.Store = DS.Store.extend({
    revision: 11,
    adapter: DS.RESTAdapter,
});

Now when I search (somewhere in my route) for a person like this

var person = App.Person.find(params);

The http://localhost/persons?post_id=10 is called. This one does not exist of course. I would've expected something like http://localhost/~me/test/persons?post_id=10. Even better would be http://localhost/~me/test/persons.php?post_id=10 How can I change this url ?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • 1
    Not sure how much the RESTAdapter has changed lately, but it used to have a property called `namespace`, so you could extend the adapter and set a global path to the resources, which in your case would be `namespace: '~/me/test'`. I don't know if it's still valid and can't find where they put it now. – MilkyWayJoe Jan 18 '13 at 20:55

3 Answers3

9

This is as of Ember Data Beta 3

To take care of the prefix, you can use the namespace property of DS.RESTAdapter. To take care of the suffix, you'll want to customize the buildURL method of DS.RESTAdapter, using _super() to get the original functionality and modifying that. It should look something like this:

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: '~me/test',
    buildURL: function() {
        var normalURL = this._super.apply(this, arguments);
        return normalURL + '.php';
    }
});
Kerrick
  • 7,420
  • 8
  • 40
  • 45
6

MilkyWayJoe is right, in your adapter you can define the namespace.

App.Adapter = DS.RESTAdapter.extend({
  namespace: '~/me/test'
});
Andre Malan
  • 2,043
  • 12
  • 12
  • Thnx, using the namespace fixes the first part of the url. What about the .php postfix. If I do it with the adapter "plurals" configuration, like: "person: persons.php" I also need to change the root element of the returned json to "persons.php". Now I don't get any errors, but the "person" returned from App.Person.find() is empty ! – Jeanluca Scaljeri Jan 19 '13 at 11:33
  • @JeanlucaScaljeri what's the format of the json you're receiving from your API? Ember expects it to be something like this `{resource_name: [{resource_id: 1,resource_column_2: "data", resource_column_n: "data"}]}` – MilkyWayJoe Jan 19 '13 at 16:01
  • App.Person.find(params) is async :( So I'll move that question to on other thread. So the only thing (which is still related to my original question) how to add the postfix .php ? – Jeanluca Scaljeri Jan 20 '13 at 19:51
  • I'm assuming just adding .php to the end of the namespace doesn't work? Do you need the .php extension in your app? Why not just rewrite the URLS in .htaccess so that your naked urls point to the php files? It would make this work and be prettier for end users. – Andre Malan Jan 20 '13 at 22:04
  • Thats true, I was just wondering, at this point it would make things easier if I could add .php. – Jeanluca Scaljeri Jan 23 '13 at 19:15
4

This would work too:

App.Person = DS.Model.extend({
    url: '~me/test/persons',
    firstName: attr('string'),
    lastName: attr('string'),
});

Or if you want to use a namespace and .php path:

App.Adapter = DS.RESTAdapter.extend({
  namespace: '~/me/test',
    plurals: {
        "persons.php": "persons.php",
    }
});

App.Person = DS.Model.extend({
    url: 'persons.php',
    firstName: attr('string'),
    lastName: attr('string'),
});

The plurals bit is to make sure Ember Data doesn't add an 's', e.g. person.phps