Skip to content

Instantly share code, notes, and snippets.

@dgeb
Last active January 3, 2016 10:08
Show Gist options
  • Save dgeb/8446998 to your computer and use it in GitHub Desktop.
Save dgeb/8446998 to your computer and use it in GitHub Desktop.
A very simple example Orbit adapter implementation for Ember Data that's hard coded with a single Orbit.LocalStorageSource. Note that the Orbit schema could easily be auto-generated from DS models. Also, DS relationships would have to be mapped to Orbit links (and the adapter would need to call `link` / `unlink` methods on Orbit stores).
export default DS.Adapter.extend({
orbitSource: undefined,
init: function() {
this._super();
Orbit.Promise = Ember.RSVP.Promise;
// TODO - autogenerate schema from DS models
var schema = {
idField: 'id',
models: {
contact: {
attributes: {
firstName: {type: 'string'},
lastName: {type: 'string'},
email: {type: 'string'},
notes: {type: 'string'}
},
links: {
phoneNumbers: {type: 'hasMany', model: 'phoneNumber', inverse: 'contact'}
}
},
phoneNumber: {
attributes: {
name: {type: 'string'}
},
links: {
contact: {type: 'hasOne', model: 'contact', inverse: 'phoneNumbers'}
}
}
}
};
var orbitSource = new Orbit.LocalStorageSource(schema);
this.set('orbitSource', orbitSource);
},
find: function(store, type, id) {
return this.get('orbitSource').find(type.typeKey, id);
},
findAll: function(store, type) {
return this.get('orbitSource').find(type.typeKey);
},
createRecord: function(store, type, record) {
var serializer = store.serializerFor(type.typeKey);
var data = serializer.serialize(record, {includeId: true});
return this.get('orbitSource').add(type.typeKey, data);
},
updateRecord: function(store, type, record) {
var serializer = store.serializerFor(type.typeKey);
var data = serializer.serialize(record, {includeId: true});
return this.get('orbitSource').update(type.typeKey, data);
},
deleteRecord: function(store, type, record) {
var serializer = store.serializerFor(type.typeKey);
var data = serializer.serialize(record, {includeId: true});
return this.get('orbitSource').remove(type.typeKey, data);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment