Skip to content

Instantly share code, notes, and snippets.

@erose
Created May 10, 2017 04:41
Show Gist options
  • Save erose/d3df9180c48299ab831bb1966e4c10e0 to your computer and use it in GitHub Desktop.
Save erose/d3df9180c48299ab831bb1966e4c10e0 to your computer and use it in GitHub Desktop.
ObjectProxy DS.HasMany
import Adapter from "ember-data/adapters/json-api";
export default Adapter.extend();
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
newPage: function(wrappedBook){
Ember.$.ajax('/change_stuff').then(
() => {
wrappedBook.get('content').reload();
}
);
}
},
});
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 DS from "ember-data";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
pages: DS.hasMany('page'),
numPages: Ember.computed('pages.[]', function() {
return this.hasMany('pages').ids().length;
}),
});
import Model from "ember-data/model";
import DS from "ember-data";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
book: DS.belongsTo('book'),
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
let i = 0;
let pagesData = [];
server.map(function() {
this.get('change_stuff', function() {
pagesData.push({ type: "pages", id: i++});
return [
200,
{'content-type': 'application/javascript'},
'',
];
});
this.get('/books/1', function() {
return json({
data: {
type: 'book',
id: 1,
relationships: {
pages: {
links: {
self: "/books/1/relationships/pages",
related: "/books/1/pages"
},
data: pagesData
}
},
},
});
});
});
const BookWrapper = Ember.ObjectProxy.extend({
//numPages: Ember.computed('pages.[]', function() {
//numPages: Ember.computed('content.pages.[]', function() {
// return this.get('content').hasMany('pages').ids().length;
//}),
});
export default Ember.Route.extend({
model: function() {
return this.store.findRecord('book', 1).then(
(book) => BookWrapper.create({ content: book })
);
},
});
import Serializer from "ember-data/serializers/json-api";
export default Serializer.extend();
<div>numPages: {{model.numPages}}</div>
<button onClick={{action 'newPage' model}}>New</button>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0",
"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": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment