Skip to content

Instantly share code, notes, and snippets.

@mistahenry
Last active November 16, 2018 13:15
Show Gist options
  • Save mistahenry/fc955f8d5d594cac0709c8d91f41a5ae to your computer and use it in GitHub Desktop.
Save mistahenry/fc955f8d5d594cac0709c8d91f41a5ae to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import { inject } from '@ember/service';
import { computed } from '@ember/object';
export default Ember.Component.extend({
modeService: inject(),
renderList: computed('modeService.mode', function(){
let mode = this.get('modeService.mode');
return mode === 'list';
})
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init(){
this._super(...arguments);
this.set('languages', [
'javascript', 'swift', 'groovy', 'haskell'
]);
}
});
import Ember from 'ember';
import { inject } from '@ember/service';
import { computed } from '@ember/object';
export default Ember.Component.extend({
modeService: inject(),
buttonText: computed('modeService.mode', function(){
let mode = this.get('modeService.mode');
return mode === 'list' ? "Change to table" : "Change to list";
}),
actions: {
toggleMode(){
let modeService = this.get('modeService');
modeService.toggleMode();
}
}
});
<button onclick={{action 'toggleMode'}}>{{buttonText}}</button>
import Ember from 'ember';
export default Ember.Service.extend({
init(){
this._super(...arguments);
//actually read from local storage here and set default on null
this.set('mode', 'list');
},
toggleMode(){
let newValue = this.get('mode') === 'list' ? 'table' : 'list';
//actually store to local storage here before caching on the service
this.set('mode', newValue);
}
});
<h1>Choose your mode</h1>
<br>
<br>
{{programming-languages languages=languages}}
{{mode-changer}}
<br>
<br>
{{#if renderList}}
<ol>
{{#each languages as |language|}}
<li>{{language}}</li>
{{/each}}
</ol>
{{else}}
<table>
<th>Language</th>
{{#each languages as |language|}}
<tr>{{language}}</tr>
{{/each}}
</table>
{{/if}}
{
"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