UNPKG

4.63 kBJavaScriptView Raw
1// This file contains a modified version of the set function from the Backbone
2// (see
3// https://github.com/jashkenas/backbone/blob/05fde9e201f7e2137796663081105cd6dad12a98/backbone.js#L460,
4// with changes below marked with an EDIT comment). This file in Backbone has the following license.
5// (c) 2010-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
6// Backbone may be freely distributed under the MIT license.
7// For all details and documentation:
8// http://backbonejs.org
9// Backbone's full license is below (from https://github.com/jashkenas/backbone/blob/05fde9e201f7e2137796663081105cd6dad12a98/LICENSE)
10/*
11Copyright (c) 2010-2015 Jeremy Ashkenas, DocumentCloud
12
13Permission is hereby granted, free of charge, to any person
14obtaining a copy of this software and associated documentation
15files (the "Software"), to deal in the Software without
16restriction, including without limitation the rights to use,
17copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the
19Software is furnished to do so, subject to the following
20conditions:
21
22The above copyright notice and this permission notice shall be
23included in all copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
27OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
29HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
30WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32OTHER DEALINGS IN THE SOFTWARE.
33*/
34import * as utils from './utils';
35import { JSONExt } from '@lumino/coreutils';
36// Set a hash of model attributes on the object, firing `"change"`. This is
37// the core primitive operation of a model, updating the data and notifying
38// anyone who needs to know about the change in state. The heart of the beast.
39// This *MUST* be called with the model as the `this` context.
40export function set(key, val, options) {
41 if (key == null) {
42 return this;
43 }
44 // Handle both `"key", value` and `{key: value}` -style arguments.
45 let attrs;
46 if (JSONExt.isObject(key)) {
47 attrs = key;
48 options = val;
49 }
50 else {
51 (attrs = {})[key] = val;
52 }
53 options || (options = {});
54 // Run validation.
55 if (!this._validate(attrs, options)) {
56 return false;
57 }
58 // Extract attributes and options.
59 const unset = options.unset;
60 const silent = options.silent;
61 const changes = [];
62 const changing = this._changing;
63 this._changing = true;
64 try {
65 if (!changing) {
66 // EDIT: changed to use object spread instead of _.clone
67 this._previousAttributes = Object.assign({}, this.attributes);
68 this.changed = {};
69 }
70 const current = this.attributes;
71 const changed = this.changed;
72 const prev = this._previousAttributes;
73 // For each `set` attribute, update or delete the current value.
74 for (const attr in attrs) {
75 val = attrs[attr];
76 // EDIT: the following two lines use our isEqual instead of _.isEqual
77 if (!utils.isEqual(current[attr], val)) {
78 changes.push(attr);
79 }
80 if (!utils.isEqual(prev[attr], val)) {
81 changed[attr] = val;
82 }
83 else {
84 delete changed[attr];
85 }
86 unset ? delete current[attr] : (current[attr] = val);
87 }
88 // Update the `id`.
89 this.id = this.get(this.idAttribute);
90 // Trigger all relevant attribute changes.
91 if (!silent) {
92 if (changes.length) {
93 this._pending = options;
94 }
95 for (let i = 0; i < changes.length; i++) {
96 this.trigger('change:' + changes[i], this, current[changes[i]], options);
97 }
98 }
99 // You might be wondering why there's a `while` loop here. Changes can
100 // be recursively nested within `"change"` events.
101 if (changing) {
102 return this;
103 }
104 if (!silent) {
105 while (this._pending) {
106 options = this._pending;
107 this._pending = false;
108 this.trigger('change', this, options);
109 }
110 }
111 }
112 finally {
113 this._pending = false;
114 this._changing = false;
115 }
116 return this;
117}
118//# sourceMappingURL=backbone-patch.js.map
\No newline at end of file