Skip to content

Instantly share code, notes, and snippets.

@jlogsdon
Forked from pangratz/adapters.application.js
Last active February 8, 2017 20:19
Show Gist options
  • Save jlogsdon/b1c297152ad29668c02e501228eb719b to your computer and use it in GitHub Desktop.
Save jlogsdon/b1c297152ad29668c02e501228eb719b to your computer and use it in GitHub Desktop.
pretender base
import Adapter from "ember-data/adapters/json-api";
export default Adapter.extend();
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";
export default Model.extend({
name: attr('string'),
value: attr('string')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr('string'),
computedValues: hasMany('computedValue')
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
const generateReport = (id) => ({
type: 'report',
id: String(id),
attributes: {}
});
const reports = [
generateReport(1),
generateReport(2),
generateReport(3),
generateReport(4),
generateReport(5),
generateReport(6),
generateReport(7),
generateReport(8),
generateReport(9),
generateReport(10)
];
server.map(function() {
this.get('/reports', function(request) {
if (request.queryParams['page[offset]'] !== '0') {
return json({data: reports.slice(3)});
} else {
return json({data: reports.slice(0, 3)});
}
});
});
export default Ember.Route.extend({
model: function() {
const reports = this.store.query('report', {
page: {limit: 3, offset: 0},
filter: {},
include: ''
});
this.store.query('report', {
page: {limit: 100, offset: 3},
filter: {},
include: ''
})
.then(result => reports.then(data => console.log(data)));
return reports;
}
});
import Serializer from "ember-data/serializers/json-api";
export default Serializer.extend();
{{#each model as |record|}}
<p>Name: {{record.id}}</p>
{{/each}}
{
"version": "0.10.4",
"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": "canary",
"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"
},
"addons": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment