Ember Data Adapters - KeynesYouDigIt/Knowledge GitHub Wiki
Adapters take requests from the store and converts them into actions for your server. They can be model-specific or application-wide.
Adapters have 7 methods to override:
-
find(store, type, id, snapshot)-GETrequest to the URL returned bybuildURL -
createRecord(store, type, snapshot)- Called bysave(). Serializes, thenPOSTs tobuildURL -
updateRecord(store, type, snapshot)- Serializes, thenPUTs tobuildURL. -
deleteRecord(store, type, snapshot)-DELETEs tobuildURL -
findAll()- Private in the REST adapter, implemented byfind() -
findQuery(store, type, query)-GETtobuildURL, with query serialized as parameters -
findMany(store, type, ids, snapshots)- Coalesces multiple find requests to oneGETrequest
-
buildURL(modelName, id, snapshot, requestType, query)- builds a URL for a request. UsesurlFor<requestType>()internally, which in turn uses_buildURL(). Builds fromhost&namespaceproperties of adapter, and themodel, andidproperties of the request. -
ajaxSuccess()/ajaxError()- Do something with response metadata -
findHasMany/findBelongsTo- Make aGETrequest to a set of unloaded records from links
buildURL: function(model, id, snapshot, requestType, query) {
var url = this._super();
if (url.toString().contains("conversations")){
url = url.replace("conversations", "me/convos");
}
return url;
}-
namespace: "api/1"- prepended to all calls -
host: "http://example.com"- override if different URL than app -
defaultSerializer: "serializerName"- override if serializer name different than adapter name -
headers: {}- hash of headers to send with every request, can be volatile
Format the server call and response. Have 3 main methods:
-
extract()- Deserializes response from server -
serialize(snapshot, options)- Converts your record to the form your server wants. Options has one boolean property:includeId. -
normalize(typeClass, hash, prop)- Formats server response into whatstore.push()wants. Transform the hash, send the normalized payload to _super().
-
extractSingle(store, typeClass, payload, id)- Called when the response is returning a single record. Restructure payload- leave more fine-grained conversion fornormalize(). Callthis._super(<signature>)to finish the method. -
extractArray(store, primaryTypeClass, payload)- Called when the response has multiple records. Otherwise the same as extractSingle. -
payloadKeyFromModelName(modelName)/modelNameFromPayloadKey(key)- If model name is different in ember than it is on the server. -
serializeIntoHash(hash, typeClass, snapshot, options)- customize root keys of the JSON -
serializePolymorphicType(snapshot, json, relationship)- Defines how polymorphic types are represented in JSON
-
normalizeHash- Takes a hash of functions to transform specific properties -
Pluralize- camelCase attributes
Use transforms for strings, numbers, dates.
normalizeResponse()normalizeFindAllResponse()normalizeFindRecordResponse()normalizeFindManyResponse()normalizeFindBelongsToResponse()normalizeFindHasManyResponse()normalizeQueryResponse()normalizeQueryRecordResponse()normalizeCreateRecordResponse()normalizeDeleteRecordResponse()normalizeUpdateRecordResponse()normalizeSaveResponse()normalizeSingleResponse()normalizeArrayResponse()
Each one is invoked on it's action and has the same interface: (store, primaryModelClass, payload, id, requestType) => JSONAPIDocument
Also:
serializeAttribute()serializeBelongsTo()serializeHasMany()
post.get('id') => postSnapshot.id
post.get('title') => postSnapshot.attr('title')
post.get('author') => postSnapshot.belongsTo('author')
post.get('comments') => postSnapshot.hasMany('comments')
post.constructor => postSnapshot.type;
post.constructor.typeKey => postSnapshot.typeKey
Underlying record is in snapshot.record
- Adapters & serializers can be per application or model
- Don’t switch APIs, switch adapters
- Use HTTP Mocks instead of fixtures
- Model shouldn’t care how it’s represented on the server
- Don’t pollute models with serializer concerns
- Refer to serialize in
normalize()
- Model hook used to use $.get()
- Then it had to map underscore to camelCase
- And then it...
- Handled in “normalize” method
- Define keys in serializer
-
typeClassis the class for a given model