Created
April 22, 2012 16:06
-
-
Save SantoshSrinivas79/2464945 to your computer and use it in GitHub Desktop.
Learning-Emberjs-Post-3-Twitter App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Namespace | |
Twitter = Em.Application.create({ | |
ready: function() { | |
// Polling | |
setInterval(function() { | |
Twitter.searchResults.refresh(); | |
}, 20000); | |
Twitter.searchResults.set("query", "Top Charts"); | |
this._super(); | |
} | |
}); | |
// Model | |
Twitter.Tweet = Em.Object.extend(); | |
// Collection | |
Twitter.searchResults = Em.ArrayController.create({ | |
content: [], | |
query: null, | |
_idCache: {}, | |
addTweet: function(tweet) { | |
var id = tweet.get("id"); | |
if (typeof this._idCache[id] === "undefined") { | |
this.pushObject(tweet); | |
this._idCache[id] = tweet.id; | |
} | |
}, | |
refresh: function() { | |
var query = this.get("query"); | |
if (Em.empty(query)) { | |
this.set("content", []); | |
return; | |
} | |
var self = this; | |
$.getJSON("http://search.twitter.com/search.json?q=" + query + "&callback=?", function(data) { | |
for (var i = 0; i < data.results.length; i++) { | |
self.addTweet(Twitter.Tweet.create(data.results[i])); | |
} | |
}); | |
}.observes("query") | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="http://dl.dropbox.com/u/10439810/ember/my_trials/js/libs/ember-0.9.7.1.min.js"></script> | |
{{noparse}} | |
<script type="text/x-handlebars"> | |
<ul class="tweets"> | |
{{#each Twitter.searchResults}} | |
<li class="tweet">{{text}}</li> | |
{{/each}} | |
</ul> | |
</script> | |
{{/noparse}} | |
<script type="text/javascript" src="http://dl.dropbox.com/u/10439810/ember/my_trials/twitter/app.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment