Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Created August 18, 2016 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlivsey/f8f8859a5fb03bb7164b86b576ff483c to your computer and use it in GitHub Desktop.
Save rlivsey/f8f8859a5fb03bb7164b86b576ff483c to your computer and use it in GitHub Desktop.
push payload
import JSONAPIAdapter from 'ember-data/adapters/json-api';
export default JSONAPIAdapter.extend({});
import Ember from 'ember';
var data = {
data: {
type: 'person',
id: 'p-1',
attributes: {
name: 'Some Person'
},
relationships: {
company: {
data: {
id: 'c-1',
type: "company"
}
}
}
},
included: [{
id: 'c-1',
type: "company",
attributes: {
name: "Foo Corp"
},
relationships: {
addresses: {
data: [{
id: 'a-1',
type: "company/address"
}, {
id: 'a-2',
type: "company/address"
}]
}
}
}, {
id: 'a-1',
type: "company/address",
attributes: {
name: "Some Place"
}
}, {
id: 'a-2',
type: "company/address",
attributes: {
name: "Some Other Place"
}
}]
};
export default Ember.Controller.extend({
store: Ember.inject.service(),
actions: {
simulateUpdate() {
Ember.run.later(this, function() {
this.get("store").pushPayload(clone(data));
}, 1000);
}
}
});
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
import Ember from 'ember';
export function now() {
return performance.now();
}
export default Ember.Helper.helper(now);
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 Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr(),
addresses: hasMany("company/address")
});
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(),
company: belongsTo("company")
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/people/p-1', function() {
return json({
data: {
type: 'person',
id: 'p-1',
attributes: {
name: 'Some Person'
},
relationships: {
company: {
data: {
id: 'c-1',
type: "company"
}
}
}
},
included: [{
id: 'c-1',
type: "company",
attributes: {
name: "Foo Corp"
},
relationships: {
addresses: {
data: [{
id: 'a-1',
type: "company/address"
}]
}
}
}, {
id: 'a-1',
type: "company/address",
attributes: {
name: "Some Place"
}
}
]
});
});
});
export default Ember.Route.extend({
store: Ember.inject.service(),
model() {
return this.get("store").findRecord("person", 'p-1');
}
});
<p>Name: {{model.name}}</p>
<p>Company: {{model.company.name}}</p>
<p>{{model.company.addresses.length}} addresses:</p>
<ul>
{{#each model.company.addresses key="id" as |address|}}
<li>{{address.name}} {{now}}</li>
{{/each}}
</ul>
{{#with model.company.addresses as |addresses|}}
{{now}}
{{/with}}
<button {{action "simulateUpdate"}}>
Simulate update from server
</button>
{
"version": "0.10.4",
"EmberENV": {
"FEATURES": {
"ds-pushpayload-return": true,
"ds-extended-errors": true
}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "canary",
"ember-data": "canary",
"ember-template-compiler": "canary",
"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"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment