Created
May 17, 2012 17:11
-
-
Save knwang/2720264 to your computer and use it in GitHub Desktop.
hugarocketeer javascript 2
This file contains 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
$(function(){ | |
var Person = Backbone.Model.extend({ | |
defaults: { | |
img_url: '', | |
name: '', | |
hugs: 0, | |
}, | |
refresh_hugs: function() { | |
var self = this; | |
$.getJSON(this.get("search_url"), function(data) { | |
var hugs = data.results.length; | |
self.set({hugs: hugs}); | |
}) | |
} | |
}); | |
var PersonView = Backbone.View.extend({ | |
tagName: "li", | |
template: _.template($('#person-template').html()), | |
initialize: function() { | |
_.bindAll(this, 'render'); | |
this.model.bind('change', this.render); | |
}, | |
render: function() { | |
$(this.el).html(this.template(this.model.toJSON())); | |
return this; | |
} | |
}); | |
var rocketeers = ['marianphelan', 'mrmicahcooper', 'p_elliott', 'knwang', 'martinisoft', 'adam_lowe', 'bthesorceror', 'higgaion', 'camerondaigle', 'ChrisCardello', 'biggunsdesign', 'daveisonthego', 'jonallured', 'joshuadavey', 'videokaz', 'mattonrails', 'mattpolito', 'therubymug', 'shaneriley', 'shayarnett', 'voxdolo', 'syntaxritual', 'johnny_rugger'].map(function(twitter_handle){ | |
return new Person({ | |
img_url: "https://api.twitter.com/1/users/profile_image?screen_name=" + twitter_handle + "&size=normal", | |
name: '@'+ twitter_handle, | |
search_url: 'http://search.twitter.com/search.json?q=' + '@'+ twitter_handle + '%20' + "%23hugarocketeer" + "&rpp=100&callback=?" | |
}); | |
}); | |
var People = Backbone.Collection.extend({ | |
model: Person, | |
comparator: function(person) { | |
return -person.get('hugs'); | |
} | |
}); | |
var AppView = Backbone.View.extend({ | |
el: $("#hug_a_rocketeer"), | |
returned_results: 0, | |
initialize: function() { | |
var self = this; | |
this.collection = new People(); | |
_.each(rocketeers, function(person) { | |
$.getJSON(person.get("search_url"), function(data) { | |
var hugs = data.results.length; | |
person.set({hugs: hugs}); | |
self.collection.add(person); | |
self.returned_results += 1; | |
if (self.returned_results == rocketeers.length) { | |
self.render(); | |
} | |
}) | |
}); | |
_.bindAll(this, 'render'); | |
}, | |
render: function(){ | |
var self = this; | |
_(this.collection.models).each(function(item){ | |
self.appendItem(item); | |
}, this); | |
}, | |
appendItem: function(item) { | |
var itemView = new PersonView({ | |
model: item | |
}); | |
$("ul#rocketeers").append(itemView.render().el); | |
}, | |
}); | |
var App = new AppView; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment