/static/js/models/sitecollection.js
JavaScript | 78 lines | 43 code | 8 blank | 27 comment | 3 complexity | 393bef7ec9242f281b95911ff6b917e6 MD5 | raw file
Possible License(s): Apache-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;
- });