| | 216 | }, |
| | 217 | |
| | 218 | // Handles requests for more themes |
| | 219 | // and caches results |
| | 220 | // |
| | 221 | // When we are missing a cache object we fire an apiCall() |
| | 222 | // which triggers events of `query:success` or `query:fail` |
| | 223 | query: function( request ) { |
| | 224 | /** |
| | 225 | * @static |
| | 226 | * @type Array |
| | 227 | */ |
| | 228 | var queries = this.queries, |
| | 229 | self = this, |
| | 230 | query; |
| | 231 | |
| | 232 | // Search the query cache for matches. |
| | 233 | query = _.find( queries, function( query ) { |
| | 234 | return _.isEqual( query.request, request ); |
| | 235 | }); |
| | 236 | |
| | 237 | // Otherwise, send a new API call and add it to the cache. |
| | 238 | if ( ! query ) { |
| | 239 | query = this.apiCall( request ).done( function( data ) { |
| | 240 | // Update the collection with the queried data. |
| | 241 | self.reset( data.themes ); |
| | 242 | |
| | 243 | // Trigger a `query:success` event |
| | 244 | // and a collection refresh event so the views are aware of changes. |
| | 245 | self.trigger( 'query:success' ); |
| | 246 | self.trigger( 'update' ); |
| | 247 | |
| | 248 | // Store the results and the query request |
| | 249 | queries.push( { themes: data.themes, request: request } ); |
| | 250 | }).fail( function() { |
| | 251 | self.trigger( 'query:fail' ); |
| | 252 | }); |
| | 253 | } else { |
| | 254 | // Only trigger an update event since we already have the themes |
| | 255 | // on our cached object |
| | 256 | this.reset( query.themes ); |
| | 257 | this.trigger( 'update' ); |
| | 258 | } |
| | 259 | }, |
| | 260 | |
| | 261 | // Local cache array for API queries |
| | 262 | queries: [], |
| | 263 | |
| | 264 | // Keep track of current query so we can handle pagination |
| | 265 | currentQuery: { |
| | 266 | apiPage: 1, |
| | 267 | request: {} |
| | 268 | }, |
| | 269 | |
| | 270 | // Send Ajax POST request to api.wordpress.org/themes |
| | 271 | apiCall: function( request ) { |
| | 272 | // Store current query request args |
| | 273 | // for later use with the event `theme:end` |
| | 274 | this.currentQuery.request = request; |
| | 275 | |
| | 276 | // Ajax request to .org API |
| | 277 | return $.ajax({ |
| | 278 | url: 'https://api.wordpress.org/themes/info/1.1/?action=query_themes', |
| | 279 | |
| | 280 | // We want JSON data |
| | 281 | dataType: 'json', |
| | 282 | type: 'POST', |
| | 283 | crossDomain: true, |
| | 284 | |
| | 285 | // Request data |
| | 286 | data: { |
| | 287 | action: 'query_themes', |
| | 288 | request: _.extend({ |
| | 289 | per_page: 36, |
| | 290 | // page: this.currentQuery.apiPage, |
| | 291 | fields: { |
| | 292 | description: true, |
| | 293 | tested: true, |
| | 294 | requires: true, |
| | 295 | rating: true, |
| | 296 | downloaded: true, |
| | 297 | downloadLink: true, |
| | 298 | last_updated: true, |
| | 299 | homepage: true, |
| | 300 | num_ratings: true |
| | 301 | } |
| | 302 | }, request) |
| | 303 | }, |
| | 304 | |
| | 305 | beforeSend: function() { |
| | 306 | // Spin it |
| | 307 | $( 'body' ).addClass( 'loading-themes' ); |
| | 308 | } |
| | 309 | }); |
| 994 | | // Send Ajax POST request to api.wordpress.org/themes |
| 995 | | themes.view.Installer.prototype.apiCall( request ).done( function( data ) { |
| 996 | | // Update the collection with the queried data |
| 997 | | self.collection.reset( data.themes ); |
| 998 | | // Trigger a collection refresh event to render the views |
| 999 | | self.collection.trigger( 'update' ); |
| 1000 | | |
| 1001 | | // Un-spin it |
| 1002 | | $( 'body' ).removeClass( 'loading-themes' ); |
| 1003 | | $( '.theme-browser' ).find( 'div.error' ).remove(); |
| 1004 | | }).fail( function() { |
| 1005 | | $( '.theme-browser' ).find( 'div.error' ).remove(); |
| 1006 | | $( '.theme-browser' ).append( '<div class="error"><p>' + l10n.error + '</p></div>' ); |
| 1007 | | }); |
| 1008 | | |
| 1009 | | return false; |
| | 1090 | // Get the themes by sending Ajax POST request to api.wordpress.org/themes |
| | 1091 | // or searching the local cache |
| | 1092 | this.collection.query( request ); |
| 1068 | | // Create a new collection with the proper theme data |
| 1069 | | // for each section |
| 1070 | | this.apiCall({ browse: section }).done( function( data ) { |
| 1071 | | // Update the collection with the queried data |
| 1072 | | self.collection.reset( data.themes ); |
| 1073 | | // Trigger a collection refresh event to render the views |
| 1074 | | self.collection.trigger( 'update' ); |
| | 1114 | // @work in progress |
| | 1115 | this.listenTo( this, 'theme:end', function() { |
| | 1116 | self.collection.currentQuery.apiPage++; |
| | 1117 | }); |
| 1161 | | // Send Ajax POST request to api.wordpress.org/themes |
| 1162 | | this.apiCall( request ).done( function( data ) { |
| 1163 | | // Update the collection with the queried data |
| 1164 | | self.collection.reset( data.themes ); |
| 1165 | | // Trigger a collection refresh event to render the views |
| 1166 | | self.collection.trigger( 'update' ); |
| 1167 | | |
| 1168 | | // Un-spin it |
| 1169 | | $( 'body' ).removeClass( 'loading-themes' ); |
| 1170 | | $( '.theme-browser' ).find( 'div.error' ).remove(); |
| 1171 | | }).fail( function() { |
| 1172 | | $( '.theme-browser' ).find( 'div.error' ).remove(); |
| 1173 | | $( '.theme-browser' ).append( '<div class="error"><p>' + l10n.error + '</p></div>' ); |
| 1174 | | }); |
| 1175 | | |
| 1176 | | return false; |
| | 1215 | // Get the themes by sending Ajax POST request to api.wordpress.org/themes |
| | 1216 | // or searching the local cache |
| | 1217 | this.collection.query( request ); |
| 1185 | | // Send Ajax POST request to api.wordpress.org/themes |
| 1186 | | this.apiCall( request ).done( function( data ) { |
| 1187 | | // Update the collection with the queried data |
| 1188 | | self.collection.reset( data.themes ); |
| 1189 | | // Trigger a collection refresh event to render the views |
| 1190 | | self.collection.trigger( 'update' ); |
| 1191 | | |
| 1192 | | // Un-spin it |
| 1193 | | $( 'body' ).removeClass( 'loading-themes' ); |
| 1194 | | $( '.theme-browser' ).find( 'div.error' ).remove(); |
| 1195 | | }).fail( function() { |
| 1196 | | $( '.theme-browser' ).find( 'div.error' ).remove(); |
| 1197 | | $( '.theme-browser' ).append( '<div class="error"><p>' + l10n.error + '</p></div>' ); |
| 1198 | | }); |
| | 1226 | // Get the themes by sending Ajax POST request to api.wordpress.org/themes |
| | 1227 | // or searching the local cache |
| | 1228 | this.collection.query( request ); |