| 18 | const globalVar = require('core/globalVar') |
| 19 | |
| 20 | class CocoModel extends Backbone.Model { |
| 21 | static initClass () { |
| 22 | this.prototype.idAttribute = '_id' |
| 23 | this.prototype.loaded = false |
| 24 | this.prototype.loading = false |
| 25 | this.prototype.saveBackups = false |
| 26 | this.prototype.notyErrors = true |
| 27 | this.schema = null |
| 28 | |
| 29 | this.prototype.attributesWithDefaults = undefined |
| 30 | |
| 31 | this.backedUp = {} |
| 32 | |
| 33 | CocoModel.pollAchievements = _.debounce(CocoModel.pollAchievements, 3000) |
| 34 | } |
| 35 | |
| 36 | constructor (attributes, options) { |
| 37 | super(...arguments) |
| 38 | if (_.isObject(attributes) && ('undefined' in attributes)) { |
| 39 | console.error(`Unsetting \`undefined\` property key during construction of ${this.constructor.className} model with value ${attributes.undefined}`) |
| 40 | delete attributes.undefined |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | initialize (attributes, options) { |
| 45 | super.initialize(...arguments) |
| 46 | if (options == null) { options = {} } |
| 47 | this.setProjection(options.project) |
| 48 | if (!this.constructor.className) { |
| 49 | console.error(`${this} needs a className set.`) |
| 50 | } |
| 51 | this.on('sync', this.onLoaded, this) |
| 52 | this.on('error', this.onError, this) |
| 53 | this.on('add', this.onLoaded, this) |
| 54 | this.saveBackup = _.debounce(this.saveBackup, 500) |
| 55 | this.usesVersions = (__guard__(__guard__(this.schema(), x1 => x1.properties), x => x.version) != null) |
| 56 | if (globalVar.application != null ? globalVar.application.testing : undefined) { |
| 57 | this.fakeRequests = [] |
| 58 | return this.on('request', function () { return this.fakeRequests.push(jasmine.Ajax.requests.mostRecent()) }) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | created () { return new Date(parseInt(this.id.substring(0, 8), 16) * 1000) } |
| 63 | |
| 64 | backupKey () { |
| 65 | if (this.usesVersions) { return this.id } else { return this.id } // + ':' + @attributes.__v # TODO: doesn't work because __v doesn't actually increment. #2061 |
| 66 | } |
| 67 | // if fixed, RevertModal will also need the fix |
| 68 | |
| 69 | setProjection (project) { |
| 70 | // TODO: ends up getting done twice, since the URL is modified and the @project is modified. So don't do this, just set project directly... (?) |
| 71 | if (project === this.project) { return } |
| 72 | let url = this.getURL() |
| 73 | if (!/project=/.test(url)) { url += '&project=' } |
| 74 | if (!/\?/.test(url)) { url = url.replace('&', '?') } |
| 75 | url = url.replace(/project=[^&]*/, `project=${(project != null ? project.join(',') : undefined) || ''}`) |
| 76 | if (!(project != null ? project.length : undefined)) { url = url.replace(/[&?]project=&/, '&') } |
| 77 | if (!(project != null ? project.length : undefined)) { url = url.replace(/[&?]project=$/, '') } |
nothing calls this directly
no outgoing calls
no test coverage detected