Isnor Creative
Isnor Creative Blog
Ruby, Ruby on Rails, Ember, Elm, Phoenix, Elixir, React, Vue

May 20, 2016

Ember Data custom adapter

I’ve been working on a few iOS/Android applications using Ember and Cordova. Both are essentially guidebooks/indexes of static content. To keep things simple, and because I did not anticipate frequent content updates, both apps were designed to contain static content stored inline in JSON.

For the first application, I used the Ember fixture adapter. It worked fine.

By the time it came to do the second application, the fixture adapter had been moved out of core to an addon, and it was advertised as being no longer suppported and not recommended. It suggested Preter as an alternative, but since testing is not my use case I wondered how much work it would be to simply concoct a custom adapter.

As it turned out, it was really very easy to get a bare minimum adapter that suited my precise requirements which for the time being involve nothing more than finding all records, and finding one record by ID.

Given that the second application is still in early stages and may become more complex – relationships? filtering? etc? – I may need to reconsider this approach –– I based the approach on the similar first application.

This was literally all there was to it, and it took a matter of minutes (seconds?) to cook up… I’m just storing storing the model data in items.

import DS from 'ember-data';

export default DS.Adapter.extend({

  findAll() {
    return this.items;
  },

  findRecord(store, type, id) {
        return this.items.filter(element => {
            return parseInt(element.id) === parseInt(id);
        })[0];        
  },

    items: [
        {"id": 1, "name":"XXX"},
        {"id": 2, "name":"YYY"},
        {"id": 3, "name":"ZZZ"}
    ]

});

I am available for Ember consulting work – get in touch to learn more.

Gordon B. Isnor

Gordon B. Isnor writes about Ruby on Rails, Ember.js, Elm, Elixir, Phoenix, React, Vue and the web.
If you enjoyed this article, you may be interested in the occasional newsletter.

I am now available for project work. I have availability to build greenfield sites and applications, to maintain and update/upgrade existing applications, team augmentation. I offer website/web application assessment packages that can help with SEO/security/performance/accessibility and best practices. Let’s talk

comments powered by Disqus