/static/js/models/sitemodel.js
JavaScript | 97 lines | 67 code | 12 blank | 18 comment | 3 complexity | 68e9c5c4e8684f31fee65c610f659925 MD5 | raw file
Possible License(s): Apache-2.0
- define([
- 'backbone',
- 'underscore',
- 'lib/objecttools',
- 'lib/urlhelper',
- 'lib/promise'
- ], function(
- Backbone,
- util,
- objectTools,
- URLHelper,
- Promise
- ){
- // Create a bound translator function.
- var translateResponseKeys = util.bind(objectTools.translate, null, {
- 'url': 'place_url',
- 'title': 'place_title',
- 'id': 'place_id',
- 'thumbnail_key': 'thumbnail_key',
- // thumbnails_job is infrequently used -- it usually comes back with a full
- // collection, rather than a single site. Really the only place we use it
- // is when a new place is added to a PlacesCollection with
- // `lattice/:username/stack/link`. Anyhow, allow it to exist.
- 'thumbnails_job': 'thumbnails_job',
- 'session_id': 'session_id',
- 'stack_id': 'stack_id'
- });
- // At this point, SiteModel is looking suspiciously like a StackModel
- // with a custom parse method and URL handler.
- // TODO: determine if we actually need SiteModel.
- var __Model = Backbone.Model.prototype;
- var SiteModel = Backbone.Model.extend({
- idAttribute: 'place_id',
- defaults: {
- status: 'ok'
- },
- initialize: function (attr, options) {
- if (!this.urlHelper) this.urlHelper = new URLHelper({
- template: config.latticeUrl + '{query}',
- tokens: {
- latticeRoot: config.latticeRoot,
- username: config.username,
- service: 'stack'
- },
- query: {
- // Ask for the v2 API by default.
- v: 2
- }
- });
- },
- url: function (tokens, query) {
- tokens = tokens || {};
- // Determine the default method based on whether this place has an ID.
- // If it doesn't have an ID, this should be a 'link' request.
- // If it does have an ID, we assume the 'link' request has already been
- // made, and issue 'warp' requests instead. Override with your own
- // method token if you like.
- util.defaults(tokens, {
- method: (this.id ? 'warp' : 'link')
- });
- return this.urlHelper.url(tokens, query);
- },
- parse: function (resp, xhr) {
- return translateResponseKeys(resp);
- },
- // Wrap `Backbone.Model.prototype.save` in a Promise.
- save: function (attrs, options) {
- options = options || {};
- var savePromise = new Promise();
- // If there is a success or error callback, add it to the thenables.
- savePromise.then(options.success, options.error);
- // Redefine success and error callbacks to resolve promise. Cool!
- options.success = function (resp) {
- savePromise.resolve(resp);
- };
- options.error = function (resp) {
- savePromise.reject(resp);
- };
- // Pow!
- __Model.save.call(this, attrs, options);
- return savePromise;
- }
- });
- return SiteModel;
- });