Skip to content

Instantly share code, notes, and snippets.

@MichalBryxi
Created July 17, 2019 13:05
Show Gist options
  • Save MichalBryxi/11d6c81a81bfcf8adbef7f84d1bb8d26 to your computer and use it in GitHub Desktop.
Save MichalBryxi/11d6c81a81bfcf8adbef7f84d1bb8d26 to your computer and use it in GitHub Desktop.
replace vs setAll
import Ember from 'ember';
import { computed } from '@ember/object';
export default Ember.Component.extend({
init() {
this._super(...arguments);
this.set('items', ['foo', 'bar', 'baz']);
},
obsers: computed('items.[]', function () {
return this.get('items').map((item) => item + "<<");
}),
actions: {
replace() {
let tmp = this.get('items');
tmp.replace(1, 1, ['unicorn']);
},
setAll() {
let tmp = this.get('items');
tmp[1] = 'unicorn';
console.log(tmp);
this.set('items', tmp);
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{my-component}}
<br>
<br>
{{#each items as |item|}}
{{item}}
{{/each}}
<hr />
{{#each obsers as |obser|}}
{{obser}}
{{/each}}
<hr />
<button {{action "setAll"}}>
set all
</button>
<button {{action "replace"}}>
replace
</button>
{
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment