1

I have JSON that is structured like this:

{
  "status": "success",
  "data": {
    "debtors": [
      {
        "debtor_id": 1048,
        "debtor_contact_id": 1086,
        ...
        ...
      },
      {
        "debtor_id": 1049,
        "debtor_contact_id": 1087,
        ...
        ...
      }
    ],
    "count": 2,
    "total": 475,
    "current_page": 1,
    "total_pages": 238,
    "page_size": 2
  }
}

I have a custom Ember-Model adapter that a coworker friend helped me write which allows me to pull a collection from the debtors key like this:

var get = Ember.get;

var ViewAdapter = Ember.RESTAdapter.extend({
    buildURL: function(klass, id) {
        var urlRoot = Ember.get(klass, 'url');
        if (!urlRoot) { throw new Error('ViewAdapter requires a `url` property to be specified'); }
        if (!Ember.isEmpty(id)) {
            return urlRoot + "/" + id;
        } else {
            return urlRoot;
        }
    },
    ajaxSettings: function(url, method) {
        return {
            url: url,
            type: method,
            headers: {
                "Accept": "application/json; version=1.0.0"
            },
            dataType: "json"
        };
    },
    didFindQuery: function(klass, records, params, data) {
        var collectionKey = get(klass, 'collectionKey'),
            dataToLoad = collectionKey ? data['data'][collectionKey] : data;

        records.load(klass, dataToLoad);
    },
    didFindAll: function(klass, records, data) {
        var collectionKey = get(klass, 'collectionKey'),
            dataToLoad = collectionKey ? data['data'][collectionKey] : data;

        records.load(klass, dataToLoad);
    }
});

export default ViewAdapter;

In the JSON there are also some pagination properties that are on the data level that i want to pull into the same model that the `debtors are going into.

This is what I want the model to look like:

import ViewAdapter from '../../adapters/accounts/view';

var attr = Ember.attr;

var View = Ember.Model.extend({
    debtor_id:                      attr(),
    debtor_contact_id:              attr(),
    ...
    ...
    current_page:                   attr(),
    total_pages:                    attr()
});

View.adapter = ViewAdapter.create();
View.url =              'api/rest/debtor/list';
View.rootKey =          'data';
View.collectionKey =    'debtors';
View.primaryKey =       'debtor_id';

export default View;

how can I change this adapter to get those properties on the data level before it goes into the debtors level?

Also, if you feel this would be better to do this in a different way, I am open to suggestions.

Grapho
  • 1,654
  • 15
  • 33
  • Personally, I wouldn't use a custom adapter, but instead use a custom serialize for the `debtors` route. Take a look here for examples (https://github.com/emberjs/data/blob/master/TRANSITION.md#rest-adapter-and-serializer-configuration). For the `data`, you can handle that with a custom `extracMeta` (http://emberjs.com/guides/models/handling-metadata/). – NicholasJohn16 Aug 21 '14 at 00:46
  • @NicholasJohn16 I was actually wondering if ember-data might be better to work with in this case... I chose ember-model because it allowed me to get it working faster (source code was smaller, quicker to understand) – Grapho Aug 21 '14 at 12:43
  • I did not notice you were using ember-model. I've never used it, but i would recommend ember-data. Emberjs and it work great together. It migjt be more work to create a custom searlizer, but i think the effort would be worth it in the long run. – NicholasJohn16 Aug 21 '14 at 21:24

0 Answers0