Skip to content

Instantly share code, notes, and snippets.

@zenom
Created November 13, 2010 16:10
Show Gist options
  • Save zenom/675448 to your computer and use it in GitHub Desktop.
Save zenom/675448 to your computer and use it in GitHub Desktop.
var Twitter = Backbone.Controller.extend({
REFRESH_INTERVAL: 10000,
el: $('#left'),
routes: {
"twitter": "twitter",
},
initialize: function() {
_.bindAll(this, 'addOne', 'addAll', 'fetchLatest');
Tweets.bind('refresh', this.addAll);
Tweets.bind('add', this.addOne);
Tweets.fetch();
this.fetchLatest();
},
addOne: function(tweet) {
//console.log('add one');
var view = new TweetView({model: tweet, 'id': tweet.attributes._id, 'className': 'tweet'});
//console.log(view);
$('#posts').prepend(view.render().el);
},
addAll: function() {
console.log('add all');
Tweets.each(function(tweet) {
var view = new TweetView({model: tweet, 'id': tweet.attributes._id, 'className': 'tweet'});
$('#posts').append(view.render().el);
});
},
fetchLatest: function() {
setInterval(function() {
Tweets.fetchLatest();
}, this.REFRESH_INTERVAL);
}
});
Twitter = Backbone.Model;
TweetList = Backbone.Collection.extend({
model: Twitter,
url: '/social/twitter/status',
QUEUE_INTERVAL: 30000,
initialize: function() {
_.bindAll(this, 'sendQueue', 'fetchLatest');
this.sendQueue();
},
comparator: function(tweet) {
console.log('comparator');
return tweet.get('created_at');
},
/* Send a job request off to the server. */
sendQueue: function() {
console.log('Sending job queue request.');
setInterval(function() {
$.getJSON('/social/twitter/status/queue', function(response) {
// Do nothing right now. But could bind this and have a trigger.
// Then unbind if we want to pause, same with the refresh model.
});
}, this.QUEUE_INTERVAL);
},
/* refresh the data from the server */
fetchLatest: function() {
collection = this;
last_id = this.first().attributes._id;
$.getJSON('/social/twitter/status/since/' + last_id, function(response) {
console.log('Found ' + response.length + ' results..');
collection.add(response);
});
}
});
Tweets = new TweetList;
TweetView = Backbone.View.extend({
template: JST.test,
initialize: function() {
_.bindAll(this, 'render');
//this.model.view = this;
console.log('Called TweetView');
},
render: function() {
$(this.el).html(this.template(this.model.toJSON())).hide().slideToggle('slow');
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment