Skip to content

Instantly share code, notes, and snippets.

@nagarajanpp8
Last active March 14, 2019 06:32
Show Gist options
  • Save nagarajanpp8/02503ac64a307c7c707d905e81e98f43 to your computer and use it in GitHub Desktop.
Save nagarajanpp8/02503ac64a307c7c707d905e81e98f43 to your computer and use it in GitHub Desktop.
count-animation using concurrency-only
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
const DEFAULT_ANIMATION_DURATION = 1000;
import DidChangeAttrs from 'ember-did-change-attrs';
export default Ember.Component.extend(DidChangeAttrs, {
startVal: 0,
didChangeAttrsConfig: {
attrs: ['endVal']
},
didInsertElement(){
this._super(...arguments);
this.get("runCountAnimation").perform();
},
didChangeAttrs(changes){
this._super(...arguments);
if(Ember.isPresent(changes) && Ember.isPresent(changes.endVal.current)){
this.set("startVal", changes.endVal.previous);
this.get("runCountAnimation").perform();
}
},
runCountAnimation: task(function *(){
let start = this.get("startVal") || 0;
let end = this.get("endVal");
let duration = end ? DEFAULT_ANIMATION_DURATION / end : DEFAULT_ANIMATION_DURATION;
duration = parseFloat(duration.toFixed(1));
duration = (duration <= 0) ? 100 : duration;
while(start <= end){
yield timeout(duration);
this.set("value", start++);
}
// Ember.run.later(()=>{
// this.sendAction("onComplete");
// }, DEFAULT_ANIMATION_DURATION - duration);
}).restartable(),
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
endVal: 3,
actions:{
update(){
let temp = this.get("endVal") + Math.floor((Math.random() * 100) + 1);
this.set("endVal", temp);
}
}
});
end value {{endVal}} <br> <button {{action "update"}}>update</button> <br>
{{count-up endVal=endVal }}
<br>
<br>
{{outlet}}
<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": "2.14.1",
"ember-template-compiler": "2.14.1",
"ember-testing": "2.14.1"
},
"addons": {
"ember-data": "3.4.2",
"ember-concurrency": "0.7.19",
"ember-did-change-attrs": "0.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment