| 94 | } |
| 95 | |
| 96 | onSaveModel (e) { |
| 97 | let key, res, val |
| 98 | const container = $(e.target).closest('.model-container') |
| 99 | const model = _.find(this.models, { id: container.data('model-id') }) |
| 100 | const treema = this.modelTreemas[model.id] |
| 101 | for (key in treema.data) { |
| 102 | val = treema.data[key] |
| 103 | if (!_.isEqual(val, model.get(key))) { |
| 104 | console.log('Updating', key, 'from', model.get(key), 'to', val) |
| 105 | model.set(key, val) |
| 106 | } |
| 107 | } |
| 108 | for (key in model.attributes) { |
| 109 | val = model.attributes[key] |
| 110 | if ((treema.get(key) === undefined) && !_.string.startsWith(key, '_')) { |
| 111 | console.log('Deleting', key, 'which was', val, 'but man, that ain\'t going to work, now is it?') |
| 112 | } |
| 113 | } |
| 114 | // model.unset key |
| 115 | const errors = model.validate() |
| 116 | if (errors) { |
| 117 | return console.warn(model, 'failed validation with errors:', errors) |
| 118 | } |
| 119 | if (!(res = model.patch())) { return } |
| 120 | res.error(() => { |
| 121 | if (this.destroyed) { return } |
| 122 | return console.error(model, 'failed to save with error:', res.responseText) |
| 123 | }) |
| 124 | return res.success((model, response, options) => { |
| 125 | if (this.destroyed) { return } |
| 126 | return this.hide() |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | destroy () { |
| 131 | for (const model in this.modelTreemas) { this.modelTreemas[model].destroy() } |