RESTAdapter: how to access resources with nested path


#1

I want to know how to create a resource which has nested path in the rest api.

for example given the end point for employee resource, /companies/{company}/employees/{employee}

How can i create a employee record. i created a employee record, but the request is going to the top level /. sending a post request with employee data to /.

can i override buildURL with a function which builds the required path for this type and calls default function for other types.

do you see any problems with this approach?


Nested URLs for Ember Data's RestAdapter proposal
#2

Well you can’t with buildURL. You have to override the Adapter.

App.YourAdapter = Ember.Mixin.create
  createRecord: (store, type, record) ->
    data = {}
    serializer = store.serializerFor(type.typeKey)
    serializer.serializeIntoHash(data, type, record, {includeId: true})

    # construct the url here
    companyId = record.get('company.id')
    url = @urlPrefix() + '/companies/' + companyId + '/' + type.typeKey
    @ajax(url, "POST", {data: data})

And use this adapter for the model.

App.EmployeeAdapter = App.ApplicationAdapter.extend App.YourAdapter