Skip to content

Instantly share code, notes, and snippets.

@Kerrick
Last active December 15, 2016 23:08
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 Kerrick/96355dbe71bf0b74958ba67858c567e9 to your computer and use it in GitHub Desktop.
Save Kerrick/96355dbe71bf0b74958ba67858c567e9 to your computer and use it in GitHub Desktop.
Reduced Test Case - Reset State
import Ember from 'ember';
export default Ember.Component.extend({
value: false,
actions: {
toggle() {
this.toggleProperty('value');
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
screen: Ember.inject.service(),
isDesktop: Ember.computed.gte('screen.width', 600),
/*
//This strategy makes you retain your state until the value of isDesktop changes, but uses observers.
watchScreenWidth: Ember.observer('screen.width', function() {
this.set('isDesktop', this.get('screen.width') >= 600);
}),
isDesktop: false,
*/
columns: Ember.computed('isDesktop', function() {
Ember.Logger.log('Recomputing columns...', new Date().toISOString());
return this.get('isDesktop') ? [[{ foo: 1 }, { foo: 3 }],[{ foo: 2 }, { foo: 4 }]] : [[{ foo: 1 },{ foo: 2 },{ foo: 3 },{ foo: 4 }]];
}),
});
<p>Toggle some items and then resize your screen (or the Ember Twiddle output panel). Even if you don't change from Desktop to Mobile, your items lose their state.</p>
<p>I thought Ember's computed property caching would mean that <code>columns</code> was not recomputed if the value of <code>isDesktop</code> didn't change, but that doesn't seem to be the case.</p>
<br>
{{screen.width}}px ({{if isDesktop "Desktop" "Mobile"}})
<br>
<div style="display: flex">
{{#each columns as |column|}}
<div>
{{#each column as |item|}}
{{item-viewer item=item}}
{{/each}}
</div>
{{/each}}
</div>
<button {{action "toggle"}}>
{{item.foo}} {{if value "on" "off"}}
</button>
{
"version": "0.10.6",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.9.0",
"ember-data": "2.9.0",
"ember-template-compiler": "2.9.0",
"ember-testing": "2.9.0"
},
"addons": {
"ember-screen": "0.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment