-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackbone.api.itunes.js
More file actions
128 lines (100 loc) · 3.32 KB
/
backbone.api.itunes.js
File metadata and controls
128 lines (100 loc) · 3.32 KB
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
/* Backbone API: iTunes
*
* Created by Makis Tracend (@tracend)
*
* Distributed through [Makesites.org](http://makesites.org)
* Released under the [MIT license](http://makesites.org/licenses/MIT)
*/
(function(_, Backbone) {
// Reference: https://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
// support the APP namespace (if available)
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;
// main request method
var iTunes = new Backbone.Model({
});
// namespace
iTunes.Models = {};
iTunes.Collections = {};
iTunes.Views = {};
// JSONP requests for all direct API requests
iTunes.Model = Model.extend({
sync : function( method, model, options ) {
options.dataType = 'jsonp';
return Backbone.sync( method, model, options );
}
});
iTunes.Collection = Collection.extend({
sync : function( method, model, options ) {
options.dataType = 'jsonp';
return Backbone.sync( method, model, options );
}
});
// **Models**: ...
iTunes.Models.Item = iTunes.Model.extend({
defaults: { },
url: function(){ return "https://itunes.apple.com/lookup?id="+ this.get("id"); },
initialize: function(){
// call cache on every state change
},
parse: function( data ){
return (data.user) ? data.user : data;
}
});
// **Collections**: ...
iTunes.Collections.Search = iTunes.Collection.extend({
model: iTunes.Models.Item,
options: {
term: "",
limit: false,
entity: false,
country: false
},
url: function(){ var url = "https://itunes.apple.com/search?term="+ this.options.term;
url += ( this.options.limit ) ? "&limit="+ this.options.term : "";
url += ( this.options.entity ) ? "&entity="+ this.options.entity : "";
url += ( this.options.country ) ? "&country="+ this.options.country : "";
return url;
},
initialize: function(){
// call cache on every state change
},
parse: function( data ){
return (data.results) ? data.results : data;
}
});
iTunes.Collections.Lookup = iTunes.Collection.extend({
model: iTunes.Models.Item,
options: {
id: false,
limit: false,
entity: false,
country: false
},
url: function(){ var url = "https://itunes.apple.com/lookup?id="+ this.options.id;
url += ( this.options.limit ) ? "&limit="+ this.options.limit : "";
url += ( this.options.entity ) ? "&entity="+ this.options.entity : "";
url += ( this.options.country ) ? "&country="+ this.options.country : "";
return url;
},
initialize: function( models, options ){
// call cache on every state change
},
parse: function( data ){
return (data.results) ? data.results : data;
}
});
// Store in selected namespace(s)
if( _.isUndefined(Backbone.API) ) Backbone.API = {};
Backbone.API.iTunes = iTunes;
// alias APP.API
if( typeof APP != "undefined" && (_.isUndefined( APP.API) || _.isUndefined( APP.API.iTunes) ) ){
APP.API = APP.API || {};
APP.API.iTunes = Backbone.API.iTunes;
}
// Shortcut
if(typeof window.iTunes == "undefined"){
window.iTunes = iTunes;
}
})(this._, this.Backbone);