Skip to content

Instantly share code, notes, and snippets.

@stavarotti
Last active October 14, 2016 02:26
Show Gist options
  • Save stavarotti/b72f9c2c7836ca32a545cf166c84505e to your computer and use it in GitHub Desktop.
Save stavarotti/b72f9c2c7836ca32a545cf166c84505e to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Component.extend({
model: [],
hasData: Ember.computed('model', {
get() {
let model = this.get('model');
return !Ember.isEmpty(model) &&
!(Ember.get(model, 'message') === 'no_data');
}
}),
tableHeadRows: Ember.computed('model', {
get() {
if (this.get('hasData')) {
return this.get('model').headerRow;
} else {
return [];
}
}
}),
tableBodyRows: Ember.computed('model', {
get() {
if (this.get('hasData')) {
return this.get('model').bodyRows;
} else {
return [];
}
}
}),
init() {
this._super(...arguments);
Ember.Logger.debug('standard-table init...');
},
didUpdateAttrs(){
this._super(...arguments);
Ember.Logger.debug('standard-table didUpdateAttrs...');
},
didReceiveAttrs(){
this._super(...arguments);
Ember.Logger.debug('standard-table didReceiveAttrs...');
},
willUpdate(){
this._super(...arguments);
Ember.Logger.debug('standard-table willUpdate...');
},
willRender(){
this._super(...arguments);
Ember.Logger.debug('standard-table willRender...');
},
didUpdate(){
this._super(...arguments);
Ember.Logger.debug('standard-table didUpdate...');
},
didRender(){
this._super(...arguments);
Ember.Logger.debug('standard-table didRender...', ...arguments);
Ember.Logger.debug('---------------------------', ...arguments);
}
});
import Ember from 'ember';
function getData () {
let time = (new Date()).getTime();
return {
headerRow: [[
{value: 'Header 1 - '},
{value: 'Header 2'}
]],
bodyRows: [[
{value: 'Cell 1 - ' + time},
{value: 'Cell 2 - ' + time}
]]
};
}
export default Ember.Controller.extend({
isLoading: false,
model: {},
actions: {
refreshData() {
this.set('isLoading', true);
Ember.run.later(this, function () {
this.set('model', getData());
this.set('isLoading', false);
}, 1000);
}
}
});
<div class="{{if isLoading 'loading'}}"></div>
<p>See output in the console. standard-table component life cycle happens twice after the second button click.</p>
<button {{action 'refreshData'}} disabled={{if isLoading 'disabled'}}>Refresh Data</button>
{{standard-table model=model}}
<table>
<thead>
<tr>
{{#each tableHeadRows as |row|}}
{{#each row as |th|}}
<th>{{th.value}}</th>
{{/each}}
{{/each}}
</tr>
</thead>
<tbody>
{{#each tableBodyRows as |row|}}
{{#each row as |td|}}
<td>{{td.value}}</td>
{{/each}}
{{/each}}
</tbody>
</table>
{
"version": "0.10.5",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.8.0",
"ember-data": "2.8.0",
"ember-template-compiler": "2.8.0",
"ember-testing": "2.8.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment