-
-
Save devotox/16b44d094aa2dbae632d38fdc3a5255e to your computer and use it in GitHub Desktop.
findRecord with slug support
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 Adapter from "ember-data/adapters/json-api"; | |
export default Adapter.extend({ | |
urlForQueryRecord({ slug }, modelName) { | |
if (slug) { | |
return this.urlForFindRecord(slug, modelName); | |
} | |
return this._super(...arguments); | |
}, | |
dataForRequest({ requestType, query }) { | |
// remove :slug query param for queryRecord, | |
// since the slug is encoded in the URL already as id | |
if (requestType === "queryRecord") { | |
delete query.slug; | |
} | |
return this._super(...arguments); | |
}, | |
// sortQueryParams needs to be used in ember-data <= 2.6, | |
// as there is no other way to modify query params | |
// the above-used dataForRequest will _probably_ be available | |
// in 2.7, when ds-improved-ajax is enabled | |
XXXsortQueryParams(options) { | |
delete options.slug; | |
return this._super(...arguments); | |
} | |
}); |
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 function returnJSON(status, body) { | |
return json(...arguments); | |
}; | |
export function json(status, body) { | |
if (arguments.length === 1) { | |
body = status; | |
status = 200; | |
} | |
return [ | |
status, | |
{ "Content-Type": "application/json" }, | |
JSON.stringify(body) | |
]; | |
}; | |
export const server = new Pretender(); | |
export function initialize() { | |
server.handledRequest = function(verb, path, request) { | |
console.log(`handled request to ${verb} ${path}`); | |
} | |
server.unhandledRequest = function(verb, path, request) { | |
console.log(`undhandled request ${verb} ${path}`); | |
} | |
}; | |
export default { | |
name: 'pretender', | |
initialize | |
}; |
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({ | |
name: attr() | |
}); |
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'; | |
import { server, json } from '../initializers/pretender'; | |
server.map(function() { | |
this.get('/people/:idOrSlug', function() { | |
return json({ | |
data: { | |
type: 'person', | |
id: 1, | |
attributes: { | |
name: 'George Michael Bluth' | |
} | |
} | |
}); | |
}); | |
}); | |
export default Ember.Route.extend({ | |
model: function() { | |
return this.store.findRecord('person', 'mr-manager'); | |
}, | |
afterModel(mrManager) { | |
let personWithId1 = this.store.peekRecord('person', 1); | |
console.log("equality: ", personWithId1 === mrManager); | |
// trigger a request in the background to show that | |
// the :slug query param is stripped correctly from | |
// the findRecord call | |
this.store.findRecord('person', 1); | |
} | |
}); |
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 Serializer from "ember-data/serializers/json-api"; | |
export default Serializer.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 Store from 'ember-data/store'; | |
export default Store.extend({ | |
init() { | |
this._super(...arguments); | |
this.modelSlugCache = Ember.MapWithDefault.create({ | |
defaultValue() { | |
return Ember.Map.create(); | |
} | |
}); | |
}, | |
_getRecords(modelName) { | |
let model = this.modelFor(modelName); | |
let records = this.typeMapFor(model).idToRecord; | |
return records; | |
}, | |
_unloadSlug(modelName, idOrSlug) { | |
let id = idOrSlug; | |
let records = this._getRecords(modelName); | |
return records && records[id] && records[id].unloadRecord(); | |
}, | |
findRecord(modelName, idOrSlug, ...args) { | |
let slugCache = this.modelSlugCache.get(modelName); | |
let id = slugCache.get(`${idOrSlug}`); | |
return id ? this._super(modelName, id, ...args) : | |
this._super(...arguments) | |
.then((record) => { | |
id = record.get('id'); | |
slugCache.set(id, id); | |
if(idOrSlug !== id) { | |
slugCache.set(idOrSlug, id); | |
this._unloadSlug(modelName, idOrSlug); | |
} | |
return record; | |
}); | |
}, | |
}); |
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.7.2", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "release", | |
"ember-data": "beta", | |
"ember-template-compiler": "release", | |
"route-recognizer": "https://rawgit.com/tildeio/route-recognizer/56f5fcec6ae58d8e86b5dc77609809fb91198142/dist/route-recognizer.js", | |
"FakeXMLHttpRequest": "https://rawgit.com/pretenderjs/FakeXMLHttpRequest/23c3a96b5b24f1bfe595867437e4f128a29c2840/fake_xml_http_request.js", | |
"pretender": "https://rawgit.com/pretenderjs/pretender/c6de9afe18b1472aded2592f5a80ad9a26a0e262/pretender.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment