Skip to content

Instantly share code, notes, and snippets.

@kumkanillam
Forked from pangratz/adapters.application.js
Created August 25, 2016 08:06
Show Gist options
  • Save kumkanillam/e21cde0ae3b9dbcc7b3311da9bcea4b2 to your computer and use it in GitHub Desktop.
Save kumkanillam/e21cde0ae3b9dbcc7b3311da9bcea4b2 to your computer and use it in GitHub Desktop.
POC store.cachedQuery()
import DS from "ember-data";
export default DS.JSONAPIAdapter.extend();
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
update() {
let persons = this.store.cachedQuery('person', {});
persons.then((result) => {
this.set("model", result);
});
}
}
});
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
};
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()
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
let count = 1;
server.map(function() {
this.get('/people', function() {
return json({
data: [{
type: 'person',
id: 1,
attributes: {
name: `George Michael Bluth, the ${count++}`
}
}]
});
}, 500);
});
export default Ember.Route.extend({
model: function() {
return this.store.cachedQuery('person', {});
}
});
import DS from "ember-data";
export default DS.JSONAPISerializer.extend();
import Ember from "ember";
import Store from "ember-data/store";
export default Store.extend({
init() {
this._super(...arguments);
let store = this;
this._queryCache = Ember.MapWithDefault.create({
defaultValue(type) {
return Ember.MapWithDefault.create({
defaultValue(query) {
return store.query(type, JSON.parse(query));
}
});
}
});
},
cachedQuery: function(type, query) {
// note that this is not be stable, so this might
// need some adaption regarding ordering of keys
let stringifiedQuery = JSON.stringify(query);
let queriesForType = this._queryCache.get(type);
let shouldUpdateQuery = queriesForType.has(stringifiedQuery);
let cachedQuery = queriesForType.get(stringifiedQuery);
// update query if it isn't newly created; this could be configurable
// via an argument to this function
if (shouldUpdateQuery) {
cachedQuery.then(function(result) {
result.update();
});
}
return cachedQuery;
}
});
Updating query: {{model.isUpdating}}
<ul>
{{#each model as |person| }}
<li>{{person.name}}</li>
{{/each}}
</ul>
<button {{action "update" }}>update</button>
{
"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": "2.5.1",
"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