Skip to content

Instantly share code, notes, and snippets.

@bantic
Last active December 4, 2019 19:24
Show Gist options
  • Save bantic/017a854cea572c1b53a682ee3cfd51ef to your computer and use it in GitHub Desktop.
Save bantic/017a854cea572c1b53a682ee3cfd51ef to your computer and use it in GitHub Desktop.
Ember 2.12.2 Set Mutations
import Ember from 'ember';
let num = 4;
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
emberVersion: Ember.VERSION,
init() {
this._super(...arguments);
this.set('theSet', new Set([1,2,3]));
},
theSetItems: Ember.computed('theSet', function() {
return Array.from(this.get('theSet'));
}),
theSetItemsBrackets: Ember.computed('theSet.[]', function() {
return Array.from(this.get('theSet'));
}),
actions: {
add1() {
let theSet = this.get('theSet');
theSet.add(num++);
},
add1AndSet() {
let theSet = this.get('theSet');
theSet.add(num++);
this.set('theSet', theSet);
},
add1AndSetNew() {
let theSet = this.get('theSet');
theSet.add(num++);
this.set('theSet', new Set(theSet));
},
notifySet() {
this.notifyPropertyChange('theSet');
},
}
});
<h1>Ember Set Mutations</h1>
<h2>Ember Version: {{emberVersion}}</h2>
<br>
<h3>theSet items (dependent key "theSet")</h3>
<ul>
{{#each theSetItems as |item|}}
<li>{{item}}</li>
{{else}}
<li><em>none</em></li>
{{/each}}
</ul>
<br> <br> <br>
<h3>theSet.[] items (dependent key "theSet.[]")</h3>
<ul>
{{#each theSetItemsBrackets as |item|}}
<li>{{item}}</li>
{{else}}
<li><em>none</em></li>
{{/each}}
</ul>
<br> <br> <br>
<button {{action "add1"}}>Mutate Set (add 1)</button> -- no observable change until "notify"
<br>
<br>
<button {{action "add1AndSet"}}>Mutate Set (add 1) and "this.set('theSet', theSet)"</button> -- no observable change until "notify"
<br>
<br>
<button {{action "add1AndSetNew"}}>Mutate Set (add 1) and "this.set('theSet', new Set(theSet)"</button> -- the change will be visible immediately because the CPs depending on "theSet" will all invalidate
<br>
<br>
<button {{action "notifySet"}}>notifyPropertyChange("theSet")</button>
<br>
<br>
{
"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": "2.12.2",
"ember-template-compiler": "2.12.2",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "2.12.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment