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 ?

1
  • 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. Commented Jan 18, 2013 at 20:55

3 Answers 3

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';
    }
});
Sign up to request clarification or add additional context in comments.

Comments

6

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

App.Adapter = DS.RESTAdapter.extend({
  namespace: '~/me/test'
});

5 Comments

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 !
@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"}]}
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 ?
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.
Thats true, I was just wondering, at this point it would make things easier if I could add .php.
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

1 Comment

I'm using ember-data 0.13 and this does not seem to be having any effect... Any idea why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.