Search code examples
javascriptember.jsember-dataurl-routing

EmberJS: How to provide a specific URL for a model in ember-data RESTAdapter?


Question 1: If I have a ember-data model called "Company", how do I tell it to hit /businesses and /businesses/:id to retrieve records instead? Is there a way to specify the url for a given model? Even better, like BackboneJS, can I compute a URL for the model at runtime?

Questions 2: I have somewhat a unique requirement, my API is organized this way:

/api/v1/company/:company_id/form/:form_id/items/:item_id

Is there a way to handle this with EmberJS? I understand ember has the DS.hasMany('App.Items') kind of relationships, but they seem to hit the /items/:item_id URL to get the data instead of the full URL.

How do I deal with this problem?


Solution

  • Answering to your first question, when you're creating your store, you can do this:

    DS.RESTAdapter.configure("plurals", {
      company: "businesses"
    });
    

    As for your second question, based on these guides I believe you can a namespace like the following, replacing the ids with your company and form information, but I haven't done this before so I can't tell for sure this would work. Assuming you land in your application knowing these ids, you could technically replace them:

    DS.RESTAdapter.reopen({
      namespace: '/api/v1/company/<replace>/form/<replace>'
    })
    

    You could also try setting the model url.

    You can read more about this here