Skip to content

Instantly share code, notes, and snippets.

@jgwhite
Created May 10, 2016 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgwhite/7a786ab059efed0d9e894cdabad070c4 to your computer and use it in GitHub Desktop.
Save jgwhite/7a786ab059efed0d9e894cdabad070c4 to your computer and use it in GitHub Desktop.
Pick Random Examples
import Ember from 'ember';
const PickRandom = Ember.Component.extend({
tagName: '',
data: null,
choice: null,
init() {
this._super();
this.pick();
},
pick() {
let data = this.get('data');
let idx = Math.floor(Math.random() * data.length);
this.set('choice', data[idx]);
}
});
PickRandom.reopenClass({
positionalParams: ['data']
});
export default PickRandom;
import Ember from 'ember';
export default Ember.Controller.extend({
data: [1,2,3,4,5,6,7,8,9,10],
choice: null,
init() {
this.pick();
},
pick() {
let data = this.get('data');
let idx = Math.floor(Math.random() * data.length);
this.set('choice', data[idx]);
}
});
Logic defined on controller:
<button onclick={{action pick}}>{{choice}}</button>
<hr>
Generic, reusable component:
{{#pick-random data as |num reload|}}
<button onclick={{action reload}}>{{num}}</button>
{{/pick-random}}
{{yield choice (action pick)}}
{
"version": "0.8.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.5.1",
"ember-data": "2.5.2",
"ember-template-compiler": "2.5.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment