Skip to content

Instantly share code, notes, and snippets.

@feanor07
Created July 24, 2019 12:33
Show Gist options
  • Save feanor07/8e3deafcac8f8858b7e4edb8ecfd8685 to your computer and use it in GitHub Desktop.
Save feanor07/8e3deafcac8f8858b7e4edb8ecfd8685 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
const DURATION = 5000
export default Ember.Component.extend({
incrementingValue: 0,
foo: Ember.observer('endVal', function(){
if (this.timer) {
Ember.run.cancel(this.timer)
}
this.startAnimation()
}),
startAnimation() {
let endVal = this.get('endVal')
let incrementingValue = this.get('incrementingValue')
if (endVal === incrementingValue) {
return
}
let range = endVal - incrementingValue
let increment = endVal > incrementingValue ? 1 : -1;
let stepTime = Math.abs(Math.floor(DURATION / range));
console.log(stepTime, increment)
this.scheduleAnimation(stepTime, increment)
},
scheduleAnimation(stepTime, increment) {
let endVal = this.get('endVal')
let incrementingValue = this.get('incrementingValue')
if (endVal === incrementingValue) {
return
}
let timer = Ember.run.later(this, function() {
let val = Math.round(incrementingValue + increment)
this.set('incrementingValue', val)
this.scheduleAnimation(stepTime, increment)
}, stepTime)
this.set('timer', timer)
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
endVal: 137,
actions: {
fireEvent() {
this.set('endVal', Math.round(Math.random()*1000))
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{my-component endVal=endVal}}
<br>
<br>
{{endVal}}
<button onclick={{action "fireEvent"}}>Fire</button>
{
"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