-
Notifications
You must be signed in to change notification settings - Fork 2
/
backbone.api.twitter.js
149 lines (98 loc) · 3.2 KB
/
backbone.api.twitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
(function(_, Backbone) {
// API root (v1.1)
var api = "https://api.twitter.com/1.1";
// conditioning the existance of the Backbone APP()
var Model = ( typeof APP != "undefined" && !_.isUndefined( APP.Model) ) ? APP.Model : Backbone.Model;
var View = ( typeof APP != "undefined" && !_.isUndefined( APP.View) ) ? APP.View : Backbone.View;
var Collection = ( typeof APP != "undefined" && !_.isUndefined( APP.Collection) ) ? APP.Collection : Backbone.Collection;
// Base model - mainly used for setup options
var Twitter = new Backbone.Model({
api: api,
token: 0,
debug: false
});
// Namespace definition
Twitter.Models = {};
Twitter.Collections = {};
Twitter.Views = {};
// JSONP requests for all direct API requests
Twitter.Model = Model.extend({
initialize: function(model, options){
options = options || {};
_.extend(this.options, options);
return Model.prototype.initialize.apply( this. arguments );
},
sync : function( method, model, options ) {
options.dataType = 'jsonp';
return Backbone.sync( method, model, options );
}
});
Twitter.Collection = Collection.extend({
initialize: function(models, options){
options = options || {};
_.extend(this.options, options);
return Collection.prototype.initialize.apply( this. arguments );
},
sync : function( method, model, options ) {
options.dataType = 'jsonp';
return Backbone.sync( method, model, options );
}
});
/* Models */
Twitter.Models.User = Twitter.Model.extend({
});
Twitter.Models.Tweet = Twitter.Model.extend({
defaults: {
}
});
/* Collections */
Twitter.Collections.Search = Twitter.Collection.extend({
model: Backbone.API.Twitter.Models.Tweet,
url: function(){ return "http://search.twitter.com/search.json?q="+ encodeURIComponent(this.options.query) +"&rpp="+ this.options.num },
options: {
query : "",
num : 10
},
parse: function( data ){
if( Twitter.get("debug") ) console.log("Twitter.Collections.Search: ", data );
return data.results;
}
});
Twitter.Collections.User = Twitter.Collection.extend({
model: Backbone.API.Twitter.Models.Tweet,
url: function(){ return api +"/statuses/user_timeline.json?screen_name=" + this.options.user + "&count="+this.options.num },
options: {
user : null,
num : 10
},
parse: function( data ){
if( Twitter.get("debug") ) console.log("Twitter.Collections.User: ", data );
return data;
}
});
/* Views */
Twitter.Views.Stream = View.extend({
initialize: function(options){
_.bindAll(this, 'render');
this.model.bind("change", this.render);
this.model.bind("reset", this.render);
this.template = Handlebars.compile( this.options.template );
},
render: function(){
var html = this.template({ items: this.model.toJSON() });
$(this.el).html( html );
}
});
// Store in selected namespace(s)
if( _.isUndefined(Backbone.API) ) Backbone.API = {};
Backbone.API.Twitter = Twitter;
// - alias APP.API
if( typeof APP != "undefined" && (_.isUndefined( APP.API) || _.isUndefined( APP.API.Twitter) ) ){
APP.API = APP.API || {};
APP.API.Twitter = Twitter;
}
// - Shortcut
if(typeof window.Twitter == "undefined"){
window.Twitter = Twitter;
}
})(this._, this.Backbone);