Last active
December 17, 2018 22:08
-
-
Save jacobq/d526982f42a741ee1379bbb76c2548c1 to your computer and use it in GitHub Desktop.
LSAdapter includes test
This file contains 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 LSAdapter from 'ember-localstorage-adapter'; | |
export default LSAdapter.extend({ | |
namespace: 'twiddle_demo_namspace' | |
}); |
This file contains 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 { 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'), | |
}); |
This file contains 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 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 }) | |
}); |
This file contains 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 Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
export default Model.extend({ | |
favoriteColor: attr('string'), | |
}); |
This file contains 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 Model from 'ember-data/model'; | |
import { hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
children: hasMany('child', { async: false }) | |
}); |
This file contains 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 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 | |
} | |
}); |
This file contains 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 { LSSerializer } from 'ember-localstorage-adapter'; | |
export default LSSerializer.extend(); |
This file contains 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.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