Ember-Data adapter for Rhom API


#1

Hello. I am trying to create an Ember Data Adapter for the Rhom API that comes as part of the Rhodes Cross Platform Mobile Development Framework

This is my code so far:

'use strict';

DS.RhomAdapter = DS.Adapter.extend(Ember.Evented, {

    extractVars: function(rhomRecord) {
        return rhomRecord.vars();
    },

    sourceIdToId: function(record) {
        record["id"] = record.source_id;
        return Ember.copy(record);
    },

    find: function(store, type, id) {
        var result = Rho.ORM.getModel(this.model).find(
            'first',
            {
                conditions: {source_id: id}
            }
        );
        return Ember.RSVP.resolve(result.vars());
    },

    findAll: function(store, type) {
        var records = Rho.ORM.getModel(this.model).find('all');
        var results = records.map(this.extractVars);
        var results = results.map(this.sourceIdToId);
        console.log(results);
        var promise = new Ember.RSVP.Promise(function(resolve, reject){
          // succeed
          resolve(results);
          // or reject
          reject([]);
        });
        return promise;
    },

    createRecord: function(store, type, record) {
        // console.log(record.toJSON());
        Rho.ORM.getModel(this.model).create(record.toJSON());
        return Ember.RSVP.resolve();
    },

    updateRecord: function(store, type, record) {
        // console.log(record.toJSON());
        var result = Rho.ORM.getModel(this.model).find('first', {conditions: {source_id: record.get('source_id')}});
        result.updateAttributes(record.toJSON());
        return Ember.RSVP.resolve();
    },

    deleteRecord: function(store, type, record) {
        var result = Rho.ORM.getModel(this.model).find('first', {conditions: {source_id: record.get('source_id')}});
        result.destroy();
        return Ember.RSVP.resolve();
    }


});

And this is my Todo model:

Todos.Todo = DS.Model.extend({
  source_id: DS.attr('string'),
  task: DS.attr('string'),
  completed: DS.attr('boolean')
});

Todos.TodoAdapter = DS.RhomAdapter.extend({
  model: "Todo"
});

I have some problems. I can see that the findAll returns an array of objects ‘with’ id (Rhom uses source_id as primary key, I have written a filter to copy it as id attribute) and I return it as a promise.

But it never gets loaded in the store (does not show up in the controller and hence in the html). But if I replace my adapter with the LocalStorage adapter it works fine.

What am I doing wrong here? Do I need to write some kinda custom serializer? I dont think there is any need. But I am a beginner. Please guide me thanks. And I have used the beta.3 Ember-Data.


#2

Anyone have a pointer for me? Please?


#3

One way is to use a custom serializer and it’s extractSingle method.

App.TodoSerializer = DS.RESTSerializer.extend({
extractSingle: function (store, type, payload, id, requestType) {
    payload.id = id; // or payload.id = payload.source_id
    delete payload.source_id;
    return this._super.apply(this, arguments);
}});

#4

Thanks a lot! Helped a ton.