Last active
December 17, 2018 19:55
-
-
Save jacobq/ce61dc11059cfe0869388b692fa95901 to your computer and use it in GitHub Desktop.
peekRecord/findRecord consistency
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-ls-namespace' | |
}); |
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 { scheduleOnce } from '@ember/runloop'; | |
import { inject as service } from '@ember/service'; | |
export default Ember.Controller.extend({ | |
store: service(), | |
myNumberFound: null, | |
myNumberPeeked: null, | |
init() { | |
this._super(...arguments); | |
scheduleOnce('afterRender', this, this._update); | |
}, | |
_update() { | |
this.store.findAll('my-model').then(records => { | |
this.set('myNumberFound', JSON.stringify(records.firstObject.myNumber)); | |
this.set('myNumberPeeked', JSON.stringify(this.store.peekAll('my-model').firstObject.myNumber)); | |
}); | |
}, | |
actions: { | |
numberChanged() { | |
console.log('numberChanged', ...arguments); | |
this.store.peekAll('my-model') | |
.map(m => m.set('myNumber', parseFloat(m.myNumber))); | |
this._update(); | |
} | |
} | |
}); |
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 { belongsTo, hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
myNumber: attr('number') | |
}); |
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'; | |
export default Ember.Route.extend({ | |
model() { | |
const store = this.get('store'); | |
return new Promise(function(resolve) { | |
store.findAll('my-model').then(records => { | |
if (records.length < 1) { | |
const newRecord = store.createRecord('my-model', { | |
myNumber: 123 | |
}); | |
newRecord.save(); | |
records = [newRecord]; | |
} | |
if (records.length > 1) { | |
console.warn('Oops, there should only ever be one record in this example'); | |
// BOOM | |
const namespace = this.adapterFor('application').namespace; | |
delete localStorage[namespace]; | |
window.location.reload(); | |
} | |
resolve(records.firstObject); | |
}) | |
}) | |
} | |
}); |
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