Last active
November 15, 2018 19:20
-
-
Save sukima/47e64fc2de362566b57895061b22b4b5 to your computer and use it in GitHub Desktop.
Ember data fu
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
import { run, later } from '@ember/runloop'; | |
import { reject } from 'rsvp'; | |
import DS from 'ember-data'; | |
const { Adapter } = DS; | |
const OUTCOMES = { | |
RESOLVE: 1, // Confirm that ember-data is working as expected | |
REJECT: 2, // ember-data should fail but does not (isRejected === false) | |
ERROR: 3 // ember-data does fail as expected (isRejected === true) | |
}; | |
// Change this variable to see the different situations. | |
const FAIL_MODE = OUTCOMES.REJECT; | |
async function notImplemented(methodName) { | |
return function() { | |
console.log(methodName, ...arguments); | |
throw new Error(`${methodName} not implemented`); | |
}; | |
} | |
export default Adapter.extend({ | |
findRecord(_, __, id) { | |
return new Promise(resolve => later(() => run(resolve), 2000)).then(() => { | |
switch (FAIL_MODE) { | |
case OUTCOMES.RESOLVE: | |
return { data: { type: 'bar', id } }; | |
case OUTCOMES.REJECT: | |
return reject(); | |
case OUTCOMES.ERROR: | |
throw new Error('Bork'); | |
} | |
}); | |
}, | |
createRecord: notImplemented('createRecord'), | |
updateRecord: notImplemented('updaterecord'), | |
deleteRecord: notImplemented('deleteRecord'), | |
findAll: notImplemented('findAll'), | |
query: notImplemented('query'), | |
queryRecord: notImplemented('queryrecord') | |
}); |
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
import DS from 'ember-data'; | |
const { Adapter } = DS; | |
async function notImplemented(methodName) { | |
return function() { | |
console.log(methodName, ...arguments); | |
throw new Error(`${methodName} not implemented`); | |
}; | |
} | |
export default Adapter.extend({ | |
async findAll() { | |
return { | |
data: [ | |
{ | |
type: 'foo', | |
id: '1', | |
relationships: { | |
bar: { data: { type: 'bar', id: '1' } } | |
} | |
}, | |
{ | |
type: 'foo', | |
id: '2', | |
relationships: { | |
bar: { data: { type: 'bar', id: '2' } } | |
} | |
}, | |
{ | |
type: 'foo', | |
id: '3', | |
relationships: { | |
bar: { data: { type: 'bar', id: '3' } } | |
} | |
} | |
] | |
}; | |
}, | |
createRecord: notImplemented('createRecord'), | |
updateRecord: notImplemented('updaterecord'), | |
deleteRecord: notImplemented('deleteRecord'), | |
findRecord: notImplemented('findRecord'), | |
query: notImplemented('query'), | |
queryRecord: notImplemented('queryrecord') | |
}); |
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
import Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import { belongsTo, hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
}); |
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
import Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import { belongsTo, hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
bar: belongsTo('bar') | |
}); |
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
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model() { | |
return this.store.findAll('foo'); | |
} | |
}); |
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
import JSONAPISerializer from 'ember-data/serializers/json-api'; | |
export default JSONAPISerializer.extend({ | |
// normalizeResponse(_, __, payload) { | |
// return payload; | |
// } | |
}); |
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
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment