Skip to content

Instantly share code, notes, and snippets.

@nightire
Last active May 23, 2020 13:05
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 nightire/884f0a6a3b29cf1a2efbe5dc14e17265 to your computer and use it in GitHub Desktop.
Save nightire/884f0a6a3b29cf1a2efbe5dc14e17265 to your computer and use it in GitHub Desktop.
Recreate vs Rerender
import Controller from '@ember/controller';
import { action, set } from '@ember/object';
import { tracked } from 'tracked-built-ins';
export default class ApplicationController extends Controller {
@tracked items = [
{ id: 1, value: 1 },
{ id: 2, value: 2 },
{ id: 3, value: 3 },
];
@action updateItems(item) {
// 1. replace the whole this.items array
// this.items = this.items.map((_item) => {
// const value = item.id === _item.id ? _item.value + 1 : _item.value;
// return { ..._item, value }
// });
// 2. replace the specified item in this.items
// const index = this.items.indexOf(item);
// this.items.replace(index, 1, [{ ...item, value: item.value + 1 }]);
// 3. update the specified property in the specified item
// set(item, 'value', item.value + 1);
// 4. same as 3. but using tracked property
// item.value = item.value + 1;
}
}
<h1>Rerender v.s. "Recreate"</h1>
<div>
<a href="https://google.com">Header Link</a>
<br>
<a href="https://google.com">Header Link</a>
<br>
<a href="https://google.com">Header Link</a>
</div>
<hr>
{{#each this.items as |item|}}
<Button @model={{item}} @updateItems={{fn this.updateItems item}} />
{{/each}}
<hr>
<div>
<a href="https://google.com">Footer Link</a>
<br>
<a href="https://google.com">Footer Link</a>
<br>
<a href="https://google.com">Footer Link</a>
</div>
<button id={{@model.id}} type="button" {{on "click" @updateItems}}>
Button {{@model.value}}
</button>
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { assign } from '@ember/polyfills';
import { start } from 'ember-qunit';
let attributes = {
rootElement: '#test-root',
autoboot: false
};
attributes = assign(attributes, config.APP);
let application = Application.create(attributes);
setApplication(application);
start();
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": true,
"_APPLICATION_TEMPLATE_WRAPPER": false,
"_JQUERY_INTEGRATION": false
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0",
"tracked-built-ins": "1.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment