Skip to content

Instantly share code, notes, and snippets.

@jacobq
Last active December 17, 2018 22:08
Show Gist options
  • Save jacobq/d526982f42a741ee1379bbb76c2548c1 to your computer and use it in GitHub Desktop.
Save jacobq/d526982f42a741ee1379bbb76c2548c1 to your computer and use it in GitHub Desktop.
LSAdapter includes test
import LSAdapter from 'ember-localstorage-adapter';
export default LSAdapter.extend({
namespace: 'twiddle_demo_namspace'
});
import Ember from 'ember';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import { inject as service } from '@ember/service';
export default Ember.Controller.extend({
store: service(),
appName: 'localStorage adapter relationship test',
init() {
this._super(...arguments);
console.log('ApplicationController -->', this);
},
showWarningMessage: false,
parents: alias('model'),
children: computed('model.@each.children.[]', function() {
const children = Array.prototype.concat(...this.model.mapBy('children').map(c => c.toArray()));
console.log('computed children; names =', children.mapBy('name'));
return children;
}),
grandchildren: computed('children.@each.children.[]', function() {
const grandchildren = Array.prototype.concat(...this.children.mapBy('children').map(c => c.toArray()));
console.log('computed grandchildren; favoriteColors =', grandchildren.mapBy('favoriteColor'));
return grandchildren;
}),
numParents: alias('parents.length'),
numChildren: alias('children.length'),
numGrandchildren: alias('grandchildren.length'),
});
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'),
children: hasMany('grandchild', { async: false })
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
favoriteColor: attr('string'),
});
import Model from 'ember-data/model';
import { hasMany } from 'ember-data/relationships';
export default Model.extend({
children: hasMany('child', { async: false })
});
import Ember from 'ember';
const names = Object.freeze([
'Alice',
'Bob',
'Carl',
'Deb',
'Eliza',
'Fred',
'George',
'Henrietta',
'Ivan',
]);
let nameIndex = 0;
const colors = Object.freeze([
'Red',
'Orange',
'Yellow',
'Green',
'Blue',
'Indigo',
'Violet',
'White',
'Black',
'Fushia',
'Teal',
'Pink',
'Cyan',
]);
let colorIndex = 0;
export default Ember.Route.extend({
model() {
console.log('model running');
return this.store.findAll('parent');
},
afterModel() {
this._super(...arguments);
console.log('afterModel running');
// Initializing store (could do elsewhere)
this.store.findAll('parent').then(parents => {
if (parents.length < 1) {
const parents = new Array(2).fill(0).map((z,i) => {
const parent = this.store.createRecord('parent', {
children: new Array(4).fill(0).map((z,i) => {
const child = this.store.createRecord('child', {
name: names[nameIndex++ % names.length],
children: new Array(2).fill(0).map((z,i) => {
const grandchild = this.store.createRecord('grandchild', {
favoriteColor: colors[colorIndex++ % colors.length]
}); // grandchild
grandchild.save();
return grandchild;
}) // grandchildren
}); // child
child.save();
return child;
}) // children
}); // parent
parent.save();
return parent;
}); // forEach
Promise.all(parents.map(p => p.save())).then(() => {
console.log('About to reload to avoid confusion related to just having primed the store.');
// FIXME: Always get stuck here since it seems we can't write to localStorage
//debugger;
//window.location.reload();
this.controllerFor('application').set('showWarningMessage', true);
});
} // if
}); // findAll
}
});
import { LSSerializer } from 'ember-localstorage-adapter';
export default LSSerializer.extend();
{{#if showWarningMessage}}
<h1 style="background-color: orange">Store initialized -- need to reload and fetch from localStorage for this demo to work</h1>
{{/if}}
<h1>{{appName}}</h1>
{{numParents}} parent(s)<br>
{{numChildren}} child(ren)<br>
{{numGrandchildren}} grandchild(ren)<br>
<ul>
{{#each parents as |parent parentIndex|}}
<li>
Parent #{{parentIndex}}
<ul>
{{#each parent.children as |child|}}
<li>
{{child.name}}
<ul>
{{#each child.children as |grandchild|}}
<li>{{grandchild.favoriteColor}}</li>
{{/each}}
</ul>
</li>
{{/each}}
</ul>
</li>
{{/each}}
</ul>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2",
"ember-localstorage-adapter": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment