/pancake-web/pancake/web/static/js/models/sitecollection.js
JavaScript | 82 lines | 43 code | 9 blank | 30 comment | 3 complexity | 1c1307f7209d2598831fc181c64c72fa MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, MIT, Apache-2.0
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- define([
- 'config',
- 'underscore',
- 'models/livecollection',
- 'models/sitemodel',
- 'models/thumbnails',
- 'lib/template',
- 'lib/urlhelper'
- ], function(
- config,
- util,
- LiveCollection,
- SiteModel,
- thumbnails,
- template,
- URLHelper
- ){
- var SiteCollection = LiveCollection.extend({
- model: SiteModel,
- url: function (tokens, query) {
- return this.urlHelper.url(tokens, query);
- },
- initialize: function (models, options) {
- // Mix in some default options.
- options = options || {};
- if (!this.urlHelper) this.urlHelper = new URLHelper({
- template: '{ base }{ path }{ query }',
- tokens: {
- base: config.apiRoot,
- path: 'top'
- },
- query: { v: 1 }
- });
- this.urlHelper.update(options.urlTokens, options.urlQuery);
- },
- // Backbone.Collection's default parse method passes sync (xhr) results
- // directly to Collection.set. We're defining a custom parse
- // implementation that massages data from our JSON API into
- // a Backbone.Model-compatible format.
- //
- // Sample return data:
- //
- // {
- // "results": [
- // {
- // "sortindex": 10,
- // "title": "Join.me",
- // "uri": "https://join.me/",
- // "thumbnail_key": "...",
- // "thumbnail_status": "processing",
- // "type": "suggestion",
- // "id": "...",
- // "types": ["suggestion"]
- // },
- // ...
- // ],
- // "success": true,
- // "thumbnails_job": "..."
- // }
- parse: function (resp, xhr) {
- resp = thumbnails.parse.call(this, resp, xhr);
- var results = resp.d;
- // nothing to do here
- if(!results.length) return [];
- // Translate response array using curried translator function.
- return util.map(results, this.model.prototype.parse);
- }
- });
-
- return SiteCollection;
- });