Skip to content

Instantly share code, notes, and snippets.

@rossanthony
Forked from pangratz/adapters.application.js
Last active November 2, 2016 16:41
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 rossanthony/1084e7750f45b034b917ecc9cd3c5cea to your computer and use it in GitHub Desktop.
Save rossanthony/1084e7750f45b034b917ecc9cd3c5cea to your computer and use it in GitHub Desktop.
pretender base
import Adapter from "ember-data/adapters/json-api";
export default Adapter.extend();
import Ember from 'ember';
export default Ember.Component.extend({
lastCheckedState: false,
lastCheckedName: '',
userInteraction: false,
actions: {
toggleAddCategory(category) {
category.toggleProperty('checked')
this.set('lastCheckedState', category.get('checked'));
this.set('lastCheckedName', category.get('name'));
console.log(category.get('name') + ' is checked?', category.get('checked'))
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
toggleAddCategory(category) {
console.log(123)
}
}
});
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(`unhandled 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(),
slug: attr(),
checked: attr()
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/categories', function() {
return json({
data: [{
type: 'category',
id: 1,
attributes: {
name: 'Foo',
slug: 'foo',
checked: false
}
},{
type: 'category',
id: 2,
attributes: {
name: 'Bar',
slug: 'bar',
checked: true
}
}]
});
});
});
export default Ember.Route.extend({
model: function() {
//return this.store.query('category');
let test = this.store.findAll('category');
console.log('test', test);
return test
}
});
import Serializer from "ember-data/serializers/json-api";
export default Serializer.extend();
{{categories-select categories=model}}
<h3>'cateogories-select' component</h3>
<p>These values update as expected here...</p>
<ul>
{{#each categories as |category|}}
<li>{{category.name}} <small>(checked = {{category.checked}})</small></li>
{{/each}}
</ul>
{{categories-tree
categories=categories
toggle=(action "toggleAddCategory")
}}
<hr>
<p>However, in Chrome the checked value that is passed into the `toggleAddCategory` action is inverted, see what is output below after checking/unchecking a checkbox above.</p>
<p style="border:1px dashed black; color:red; padding:10px">
{{#if lastCheckedName}}
You just <b>{{if lastCheckedState 'checked' 'unchecked'}}</b>
{{lastCheckedName}}
{{else}}
Try checking/unchecking a checkbox above to see this update
{{/if}}
&nbsp;</p>
<p>You can also see this in the console log, where the value of category.get('checked') is flipped in Chrome and FF, but fine in Safari.</p>
<input type="checkbox"
id={{concat elementId '-' category.slug}}
checked={{category.checked}}
onchange={{action toggle category}}
>
<label for="{{concat elementId '-' category.slug}}">
{{category.name}} <small>(checked = {{category.checked}})</small>
</label>
{{#if hasChildren}}
{{categories-tree
categories=children
toggle=(action toggle)}}
{{/if}}
<hr>
<h3>'cateogories-tree' component</h3>
{{#each categories as |category|}}
{{categories-tree-node
category=category
toggle=(action toggle)
}}
{{/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