Skip to content

Instantly share code, notes, and snippets.

@diolektor
Created January 24, 2013 14:45
Show Gist options
  • Save diolektor/4622494 to your computer and use it in GitHub Desktop.
Save diolektor/4622494 to your computer and use it in GitHub Desktop.
App = Ember.Application.create();
var TableDataBuild = Ember.Object.extend({
items: [],
content: null,
_callChangeContent: null,
_object: null,
buildObject: function() {
var properties = {};
columns.forEach(function(item) {
properties[item] = null;
});
this._object = Ember.Object.extend(properties);
},
groupByProperty: function(property, algorithm) {
var self = this;
self._callChangeContent = 'groupByProperty';
var result = {};
content.forEach(function(item, i) {
var p = item[property];
if (typeof result[p] === undefined) {
result[p] = self._object.create(item.getProperties(self.columns));
} else {
self.columns.forEach(function(key) {
if (typeof self[key] === 'function') {
result[p].set(key, self.key.call(result[p]));
} else {
result[p].set(key, result[p].get(key) + item[key]);
}
});
}
});
},
pct_value: function(part, full) {
var res = Math.round(part / full * 1000)/10;
if ( isNaN(res) || !isFinite(res) ) { return '—'; }
res += '';
if ( res.indexOf('.') == -1 ) { res += '.0'; }
return res + '%';
},
changeContent: function('content') {
if (typeof this[this.get('_callChangeContent')] === 'function') {
this[this.get('_callChangeContent')].call();
}
}.observes('content')
});
var dataTable = TableDataBuild.create({
contentBinding : [],
columns : ['date', 'click', 'regs', 'regs_click', 'nu', 'nu_regs', 'au', 'au_nu', 'pu', 'pu_au'],
regs_click : function(row) {
return this.pct_value(row.regs, row.click);
},
nu_regs : function(row) {
return this.pct_value(row.nu, row.regs);
},
au_nu : function(row) {
return this.pct_value(row.au, row.nu);
},
pu_au : function(row) {
return this.pct_value(row.pu, row.au);
}
}).groupByProperty('date')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment