Skip to content

Instantly share code, notes, and snippets.

@ZOwl
Created September 20, 2013 15:25
Show Gist options
  • Save ZOwl/6639232 to your computer and use it in GitHub Desktop.
Save ZOwl/6639232 to your computer and use it in GitHub Desktop.
.number-counter {
color: #707070;
text-align: center;
float: left;
margin: 20px;
}
.tweets {
color: #0084b4;
}
.likes {
color: #3b5998;
}
.pins {
color: #cb2027;
}
.count {
font-size: 30px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Number Counter with Ember Component</title>
<link rel="stylesheet" href="https://rawgithub.com/emberjs/starter-kit/v1.0.0/css/normalize.css">
</head>
<body>
<script type="text/x-handlebars">
{{number-counter duration=100 content=content item="likes"}}
{{number-counter duration=150 content=content item="tweets"}}
{{number-counter duration=200 content=content item="pins"}}
</script>
<script type="text/x-handlebars" id="components/number-counter">
<div {{bindAttr class="item :count"}}>{{count}}</div>
<label>{{item}}</label>
</script>
<script src="https://rawgithub.com/emberjs/starter-kit/v1.0.0/js/libs/jquery-1.9.1.js"></script>
<script src="https://rawgithub.com/emberjs/starter-kit/v1.0.0/js/libs/handlebars-1.0.0.js"></script>
<script src="https://rawgithub.com/emberjs/starter-kit/v1.0.0/js/libs/ember-1.0.0.js"></script>
</body>
</html>
App = Em.Application.create();
App.ApplicationController = Em.Controller.extend({
content: Em.Object.create({
likes: 100, tweets: 300, pins: 0
})
});
App.NumberCounterComponent = Em.Component.extend({
classNames: 'number-counter',
count: 0,
getData: function(item){
// you can replace getting data from server
setInterval(function() {
this.set(item, this.get(item)+10);
}.bind(this), this.get('duration')*20);
},
addCounter: function(item){
// observe the content's item to increase number
this.addObserver(item, function(){
var counter = setInterval(function(){
var oldCount = this.get('count'),
newCount = this.get(item);
if (oldCount<newCount){
this.incrementProperty('count', 1);
return;
}
clearInterval(counter);
}.bind(this), this.get('duration'));
});
},
startCounter: function() {
var item = 'content.'+this.get('item');
this.set('count', this.get(item));
this.getData(item);
this.addCounter(item);
}.on('didInsertElement')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment