@@ -1,17 +1,28 @@
1-$(function () {
2- Tumblr = Backbone.Collection.extend({
1+var xonecas = !function () {
2+
3+ function linkit (text) {
4+ text = text.replace(
5+ /(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,
6+ '<a href="$&" target="_new">$&</a>');
7+ text = text.replace(/@\w+\s/gi, function (match) {
8+ match = match.substring(0, match.length -1);
9+ return '<a href="//twitter.com/'+match.substring(1)+'">'+match+'</a> ';
10+ });
11+
12+ return text;
13+ }
14+
15+ var Tumblr = Backbone.Collection.extend({
316 url: "http://api.tumblr.com/v2/blog/xonecas.tumblr.com/posts/text",
417 page: 0,
5- count: 6,
18+ count: 5,
619 idx: 0,
720
821 initialize: function () {
9- _.bindAll(this, "setCurrent");
10- this.bind('reset', this.setCurrent);
1122 },
1223
1324 parse: function (res, xhr) {
14- this.total = res.response.total_posts;
25+ this.total = Math.ceil(res.response.total_posts/this.count);
1526 return res.response.posts;
1627 },
1728
@@ -30,95 +41,82 @@ $(function () {
3041 return $.ajax(params);
3142 },
3243
33- setCurrent: function () {
34- this.current = this.at(this.idx);
35- },
36-
3744 next: function () {
38- this.idx++;
39- if (this.idx + (this.count * this.page) === this.total) {
40- this.trigger('error', 'At oldest post.');
41- this.idx--;
42- return;
43- }
44-
45- var model = this.at(this.idx);
46-
47- if (model) {
48- this.current = model;
49- this.trigger('change');
45+ if (this.page +1 === this.total) {
46+ this.trigger('error', "That's it, you've read it all!");
5047 } else {
5148 this.page++;
52- this.idx = 0;
5349 this.fetch();
5450 }
5551 },
5652
5753 previous: function () {
58- this.idx--;
59- var model = this.at(this.idx);
60-
61- if (model) {
62- this.current = model;
63- this.trigger('change');
54+ if (this.page -1 === -1) {
55+ this.trigger('error', "No newer posts.");
6456 } else {
65- if (this.page > 0) {
66- this.page--;
67- this.idx = this.count -1;
68- this.fetch();
69- } else {
70- this.idx++;
71- this.trigger('error', 'At most recent post.');
72- }
57+ this.page--;
58+ this.fetch();
7359 }
74-
7560 }
7661 });
7762
78- HomePage = Backbone.View.extend({
79- id: 'main',
80- className: 'container',
81- tagName: 'section',
82- template: Handlebars.compile($('#post_tmpl').html()),
83- collection: new Tumblr(),
63+ var Twitter = Backbone.Collection.extend({
64+ url: "http://api.twitter.com/1/statuses/user_timeline.json",
65+ count: 5,
8466
85- initialize: function () {
86- this.header = new Header({
87- 'collection': this.collection
67+ parse: function (res) {
68+ _.each(res, function (tweet) {
69+ tweet.text = linkit(tweet.text);
8870 });
89- this.header.render();
90- _.bindAll(this, "render", "report");
91- this.collection.bind('reset', this.render);
92- this.collection.bind('error', this.report);
93- this.collection.bind('change', this.render);
94- this.collection.fetch();
95-
96- $('body').append(this.el);
71+
72+ return res;
9773 },
9874
99- report: function (err) {
100- smoke.alert(err);
75+ sync: function (model, method, options) {
76+ var params = _.extend(options, {
77+ "url": this.url,
78+ "data": {
79+ "screen_name": "xonecas",
80+ "count": this.count,
81+ "trim_user": true,
82+ "include_rts": true,
83+ "exclude_replies": true
84+ },
85+ "dataType": "jsonp"
86+ });
87+
88+ return $.ajax(params);
89+ }
90+ });
91+
92+ Badges = Backbone.Collection.extend({
93+ url: "http://coderwall.com/xonecas.json",
94+
95+ parse: function (res, xhr) {
96+ return res.data.badges;
10197 },
98+
99+ sync: function (model, method, options) {
100+ var params = _.extend(options, {
101+ "url": this.url,
102+ "dataType": "jsonp"
103+ });
102104
103- render: function () {
104- var model = this.collection.current;
105- $(this.el)
106- .css('display', 'none')
107- .html(this.template(model.toJSON()))
108- .fadeIn(1000);
109- hijs('code');
105+ return $.ajax(params);
110106 }
111107 });
112108
113- Header = Backbone.View.extend({
109+
110+ var Header = Backbone.View.extend({
114111 className: 'topbar',
115- tagName: 'div',
116- template: $('#header_tmpl').html(),
112+ tagName: 'header',
113+ template: '#header_tmpl',
117114
118115 render: function () {
119116 $(this.el)
120- .html(this.template)
121- .appendTo('body');
117+ .html($(this.template).html())
118+ .appendTo('body')
119+ .dropdown();
122120 },
123121
124122 events: {
@@ -139,6 +137,81 @@ $(function () {
139137 }
140138 });
141139
142- // main
143- window.app = new HomePage();
144-});
140+ var Footer = Backbone.View.extend({
141+ className: 'container',
142+ tagName: 'footer',
143+ template: '#footer_tmpl',
144+
145+ initialize: function () {
146+ _.bindAll(this, "render");
147+ this.twitter = new Twitter();
148+ this.twitter.bind('reset', this.render);
149+ this.twitter.fetch();
150+
151+ this.badges = new Badges();
152+ this.badges.bind('reset', this.render);
153+ this.badges.fetch();
154+ },
155+
156+ render: function () {
157+ var
158+ template = Handlebars.compile($(this.template).html()),
159+ context = {
160+ twitter: this.twitter.toJSON(),
161+ badges: this.badges.toJSON()
162+ };
163+
164+ $(this.el)
165+ .html(template(context))
166+ .appendTo('body');
167+ }
168+ });
169+
170+ var Page = Backbone.View.extend({
171+ id: 'main',
172+ className: 'container',
173+ tagName: 'section',
174+ template: '#post_tmpl',
175+ collection: new Tumblr(),
176+
177+ initialize: function () {
178+ this.header = new Header({
179+ 'collection': this.collection
180+ });
181+ this.footer = new Footer();
182+ _.bindAll(this, "render", "report");
183+ this.collection.bind('reset', this.render);
184+ this.collection.bind('error', this.report);
185+ this.collection.bind('change', this.render);
186+ this.collection.fetch();
187+
188+ this.header.render();
189+ $('body').append(this.el);
190+ this.footer.render();
191+ },
192+
193+ report: function (err) {
194+ smoke.alert(err);
195+ },
196+
197+ render: function () {
198+ var
199+ html = "",
200+ template = Handlebars.compile($(this.template).html());
201+
202+ this.collection.each(function (model) {
203+ html += template(model.toJSON());
204+ });
205+
206+ $(this.el)
207+ .css('display', 'none')
208+ .html(html)
209+ .fadeIn(1000);
210+ hijs('code');
211+ }
212+ });
213+
214+ $(function () {
215+ window.page = new Page();
216+ });
217+} ();