Skip to content

Instantly share code, notes, and snippets.

@k-fish
Created November 29, 2018 20:41
Show Gist options
  • Save k-fish/96ddcc44ea89e2c9ad20ff2eafceee5e to your computer and use it in GitHub Desktop.
Save k-fish/96ddcc44ea89e2c9ad20ff2eafceee5e to your computer and use it in GitHub Desktop.
Example Random Collections
import Ember from 'ember';
export default Ember.Component.extend({
listOfTranslations: [
'Foo',
'Bar',
'Baz'
]
});
import { helper } from '@ember/component/helper';
export function randomElement([collection]) {
if (!collection.length) {
return;
}
return collection[parseInt(Math.random() * collection.length)];
}
export default helper(randomElement)
<h1>Example Random Collections</h1>
<br>
<br>
{{my-component}}
<br>
<br>
{{yield}}
Example of random choice from list {{listOfTranslations}}
<br>
<br>
Choice: <strong>{{random-element listOfTranslations}}</strong>
<br>
(refresh to see randomness)
<br>
<br>
{
"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