Last active
January 3, 2016 10:08
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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