Skip to content

Instantly share code, notes, and snippets.

@rossjha
Last active February 22, 2018 14:59
Show Gist options
  • Save rossjha/61f2042812b8cd915afbcaea142ceb0f to your computer and use it in GitHub Desktop.
Save rossjha/61f2042812b8cd915afbcaea142ceb0f to your computer and use it in GitHub Desktop.
Experiment Split
import Ember from 'ember';
import _math from 'lodash/math';
const { computed, get } = Ember;
export default Ember.Controller.extend({
ratios: computed.mapBy('experiments', 'ratio'),
totalofExperimentRatios: computed.sum('ratios'),
controlRatio: computed('totalofExperimentRatios', function () {
return this._controlRatio();
}),
_controlRatio() {
let total = get(this, 'totalofExperimentRatios');
let ratio = 100 - total;
if (ratio < 1) {
return 0;
}
if (!isNaN(ratio)) {
if (!Number.isInteger(ratio)) {
console.log(ratio);
return _math.round((ratio), 2).toFixed(1);
}
return ratio;
}
},
experiments: [{
name: 'A',
ratio: 33.33
}, {
name: 'B',
ratio: 33.33
}]
});
<div>control {{controlRatio}}</div>
<div>total {{totalofExperimentRatios}}</div>
{{#each experiments as |experiment|}}
<div>
{{experiment.name}} {{experiment.ratio}}
</div>
{{/each}}
{
"version": "0.13.0",
"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.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3",
"ember-lodash": "4.17.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment