0

The cloudant RESTful API is fairly simple but doesn't match the way ember-data expects things to be. How can I customize or create an Adapter that deals with these issues...

In my specific case I only want to load records from one of several secondary indexes (ie. MapReduce fnctions).

The URL for this is below, where [name] and [view] would change depending on user selection or the route I am in.

https://[username].cloudant.com/[db_name]/_design/[name]/_view/[view]

Looking at the ember-data source there doesn't seem to be an easy way of defining URLs like this. I took a look at findQuery and it expects to send any variables through as url params not as part of the actual URL itself.

Am I missing something? Is there an obvious way of dealing with this?

Then the data comes back in a completely different format, is there a way to tell ember what this format is?

Thanks!

4

1 回答 1

2

I had similar problem where URL's are dynamic. I ended up creating my own adapater by extending DS.RESTAdapter and overriding the default buildURL method. For example:

App.MyAdapter = DS.RESTAdapter.extend({
  buildURL: function(record, suffix) {
    var username, db_name, name, view;
    // Do your magic and fill the variables
    return 'https://'+username+'.cloudant.com/'+db_name+'/_design/'+name+'/_view/'+view;
  }
});

I ended up also defining my own find, findAll, findQuery, createRecord, updateRecord, deleteRecord etc. methods as I had to pass more variables to buildURL method.

If returning data is in different format then you can also write your own serializer by extending DS.JSONSerializer and define your own extraction methods extract, extractMany etc.

You should evaluate how well your API follows the data format required by ember/data RESTAdapter. If it is very different then it's maybe better to use some other component for communication like ember-model, ember-restless, emu etc, as ember-data is not very flexible (see this blog post). You can also write your own ajax queries directly from routes model hooks without using ember-data or other components at all. It is not very hard to do that.

于 2013-07-10T09:35:43.577 回答