-
-
Save rstudner/c9a65f6bd6131246dd84cbda036c3471 to your computer and use it in GitHub Desktop.
ref helper
This file contains hidden or 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 DS from "ember-data"; | |
export default DS.JSONAPIAdapter.extend(); |
This file contains hidden or 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'; | |
const { | |
inject, | |
Helper | |
} = Ember; | |
class Ref extends String { | |
constructor(id, type) { | |
super(id); | |
this.type = type; | |
this.id = id; | |
} | |
unknownProperty(key) { | |
throw new Error(`You accessed the property '${key}' on a reference for ${this.type}:${this.id}, but only 'id' and 'type' are available on references.`); | |
} | |
toString() { | |
return `${this.id}`; | |
} | |
valueOf() { | |
return this.id; | |
} | |
} | |
export default Ember.Helper.extend({ | |
store: inject.service('store'), | |
compute([model, relName]) { | |
let rel = model.relationshipFor(relName); | |
if (!rel) { | |
throw new Error(`No such relationship '${relName}' on ${model}`); | |
} | |
let type = rel.type; | |
if (rel.kind === 'belongsTo') { | |
rel = model.belongsTo(relName).value(); | |
return new Ref(rel.id, relName); | |
} | |
let arr = model.hasMany(relName).value().currentState; | |
let len = arr.length; | |
let refs = new Array(len); | |
for (let i = 0; i < len; i++) { | |
refs[i] = new Ref(arr[i].id, type); | |
} | |
return refs; | |
} | |
}); |
This file contains hidden or 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 hidden or 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 DS from "ember-data"; | |
const { Model, attr, belongsTo, hasMany } = DS; | |
export default Model.extend({ | |
name: attr(), | |
siblings: hasMany('person', { inverse: null }), | |
father: belongsTo('person', { inverse: null }) | |
}); |
This file contains hidden or 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 config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('person', { path: 'person/:id' }); | |
}); | |
export default Router; |
This file contains hidden or 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'; | |
export default Ember.Route.extend({ | |
model: function() { | |
this.store.push({ | |
data: { | |
id: 1, | |
type: 'person', | |
attributes: { | |
name: "George Michael Bluth" | |
}, | |
relationships: { | |
siblings: { | |
data: [ | |
{ id: 2, type: 'person' }, | |
{ id: 3, type: 'person' }, | |
{ id: 4, type: 'person' }, | |
{ id: 5, type: 'person' } | |
] | |
}, | |
father: { | |
data: { id: 6, type: 'person' } | |
} | |
} | |
}, | |
included: [ | |
{ | |
id: 2, | |
type: 'person', | |
attributes: { | |
name: "Maeby Fünke 1" | |
} | |
}, | |
{ | |
id: 3, | |
type: 'person', | |
attributes: { | |
name: "Maeby Fünke 2" | |
} | |
}, | |
{ | |
id: 4, | |
type: 'person', | |
attributes: { | |
name: "Maeby Fünke 3" | |
} | |
}, | |
{ | |
id: 5, | |
type: 'person', | |
attributes: { | |
name: "Maeby Fünke 4" | |
} | |
}, | |
{ | |
id: 6, | |
type: 'person', | |
attributes: { | |
name: "Papa Bluth" | |
} | |
} | |
] | |
}); | |
return this.store.peekRecord('person', 1); | |
} | |
}); |
This file contains hidden or 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'; | |
export default Ember.Route.extend({ | |
model(params) { | |
return this.get('store').findRecord('person', params.id); | |
} | |
}); |
This file contains hidden or 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 DS from "ember-data"; | |
import Ember from 'ember'; | |
export default Ember.Object.extend({ | |
normalizeResponse(_, __, payload) { | |
return payload; | |
} | |
}); | |
/* | |
export default DS.JSONAPISerializer.extend({ | |
keyForRelationship(key) { | |
return key; | |
} | |
}); | |
*/ |
This file contains hidden or 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": "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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment