Skip to content

Instantly share code, notes, and snippets.

@mistahenry
Forked from abueloshika/components.my-component.js
Last active January 16, 2019 16:38
Show Gist options
  • Save mistahenry/fa2ecce9c2703596a1fe3e89823f57d4 to your computer and use it in GitHub Desktop.
Save mistahenry/fa2ecce9c2703596a1fe3e89823f57d4 to your computer and use it in GitHub Desktop.
component demo
import Ember from 'ember';
import {
computed
} from '@ember/object';
export default Ember.Component.extend({
init() {
this._super(...arguments);
this.dataSorting = ['total_customers']
this.dataSortingDesc = ['total_customers:desc']
},
sortedDataDesc: computed.sort('unsortedData', 'dataSortingDesc'),
sortedData: computed.sort('unsortedData', 'dataSorting'),
unsortedData: computed('model', function () {
return this.get('model');
}),
actions: {
columnsort(property) {
if (!this.get('tableSorted')) {
this.set('dataSortingDesc', [`${property}:desc`])
this.set('model', this.get('sortedDataDesc'))
this.set('tableSorted', true)
} else {
this.set('dataSorting', [property])
this.set('model', this.get('sortedData'))
this.set('tableSorted', null)
}
},
}
});
import Ember from 'ember';
export default Ember.Component.extend({
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
updateFrank(){
let model = this.get('model');
let frank = model.find((it) => it.name === 'Frank');
if(frank){
frank.set('age', 122);
}
}
}
});
import Ember from 'ember';
import EmberObject from '@ember/object';
export default Ember.Route.extend({
model(){
return [EmberObject.create({name: "Frank", age: 22}), EmberObject.create({name: "Alan", age: 43}), EmberObject.create({name: "Bob", age: 56})]
},
setupController(controller, model){
controller.set('model', model);
controller.set('tableModel', model.slice(0));
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{my-component model=tableModel}}
{{second-component model=model}}
<br>
<br>
<button {{action 'updateFrank'}}>Update Frank</button>
<table>
<thead>
<th {{action "columnsort" "name"}}>Name</th>
<th {{action "columnsort" "age"}}>Age</th>
</thead>
<tbody>
{{#each model as |model|}}
<tr>
<td>
{{model.name}}
</td>
<td>{{model.age}}</td>
</tr>
{{/each}}
</tbody>
</table>
{{#each model as |model|}}
<ul>
<li>{{model.name}} - {{model.age}} </li>
</ul>
{{/each}}
{
"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