Skip to content

Instantly share code, notes, and snippets.

@SantoshSrinivas79
Created April 22, 2012 16:06
Show Gist options
  • Save SantoshSrinivas79/2464945 to your computer and use it in GitHub Desktop.
Save SantoshSrinivas79/2464945 to your computer and use it in GitHub Desktop.
Learning-Emberjs-Post-3-Twitter App
// 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")
});
<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