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.