Sometimes you want a variable that is persistent as long as the page is not reloaded. Since a lot of the Ember convention is extending objects, I'm not sure where to put private variables so that they are accessible from public functions.
It seems obvious to just put it in the extend()ed object, but then the variable isn't reachable from within a function in that same object, probably because Ember doesn't bind() the parent object to the function.
Here's an example of how I store an array so that I can deduct if my RESTAdapter should reload a record that has been requested before:
shouldReloadRecord: function(store, snapArray) {
window.recordHashStore = window.recordHashStore || [];
let recordHash = `${snapArray.modelName}/${snapArray.id}?include=${snapArray.include}`;
if (~window.recordHashStore.indexOf(recordHash)) {
window.recordHashStore.push(recordHash);
console.log('Reloading', snapArray.type);
return true;
}
else {
console.log('NOT reloading', snapArray.type);
return false;
}
}
the relevant code here is window.recordHashStore = window.recordHashStore || []. This does exactly what I want, but it doesn't look very Ember'y. Also it's not nice to use the window global scope. Where can I store private variables for the duration of the app in the browser, so that it's available and not overwritten on transitions?
Aucun commentaire:
Enregistrer un commentaire