@@ -76,58 +76,65 @@ export function set(
76
76
const changes = [ ] ;
77
77
const changing = this . _changing ;
78
78
this . _changing = true ;
79
-
80
- if ( ! changing ) {
81
- // EDIT: changed to use object spread instead of _.clone
82
- this . _previousAttributes = { ...this . attributes } ;
83
- this . changed = { } ;
84
- }
85
-
86
- const current = this . attributes ;
87
- const changed = this . changed ;
88
- const prev = this . _previousAttributes ;
89
-
90
- // For each `set` attribute, update or delete the current value.
91
- for ( const attr in attrs ) {
92
- val = attrs [ attr ] ;
93
- // EDIT: the following two lines use our isEqual instead of _.isEqual
94
- if ( ! utils . isEqual ( current [ attr ] , val ) ) {
95
- changes . push ( attr ) ;
96
- }
97
- if ( ! utils . isEqual ( prev [ attr ] , val ) ) {
98
- changed [ attr ] = val ;
99
- } else {
100
- delete changed [ attr ] ;
79
+ try {
80
+ if ( ! changing ) {
81
+ // EDIT: changed to use object spread instead of _.clone
82
+ this . _previousAttributes = { ...this . attributes } ;
83
+ this . changed = { } ;
101
84
}
102
- unset ? delete current [ attr ] : ( current [ attr ] = val ) ;
103
- }
104
85
105
- // Update the `id`.
106
- this . id = this . get ( this . idAttribute ) ;
107
-
108
- // Trigger all relevant attribute changes.
109
- if ( ! silent ) {
110
- if ( changes . length ) {
111
- this . _pending = options ;
86
+ const current = this . attributes ;
87
+ const changed = this . changed ;
88
+ const prev = this . _previousAttributes ;
89
+
90
+ // For each `set` attribute, update or delete the current value.
91
+ for ( const attr in attrs ) {
92
+ val = attrs [ attr ] ;
93
+ // EDIT: the following two lines use our isEqual instead of _.isEqual
94
+ if ( ! utils . isEqual ( current [ attr ] , val ) ) {
95
+ changes . push ( attr ) ;
96
+ }
97
+ if ( ! utils . isEqual ( prev [ attr ] , val ) ) {
98
+ changed [ attr ] = val ;
99
+ } else {
100
+ delete changed [ attr ] ;
101
+ }
102
+ unset ? delete current [ attr ] : ( current [ attr ] = val ) ;
112
103
}
113
- for ( let i = 0 ; i < changes . length ; i ++ ) {
114
- this . trigger ( 'change:' + changes [ i ] , this , current [ changes [ i ] ] , options ) ;
104
+
105
+ // Update the `id`.
106
+ this . id = this . get ( this . idAttribute ) ;
107
+
108
+ // Trigger all relevant attribute changes.
109
+ if ( ! silent ) {
110
+ if ( changes . length ) {
111
+ this . _pending = options ;
112
+ }
113
+ for ( let i = 0 ; i < changes . length ; i ++ ) {
114
+ this . trigger (
115
+ 'change:' + changes [ i ] ,
116
+ this ,
117
+ current [ changes [ i ] ] ,
118
+ options
119
+ ) ;
120
+ }
115
121
}
116
- }
117
122
118
- // You might be wondering why there's a `while` loop here. Changes can
119
- // be recursively nested within `"change"` events.
120
- if ( changing ) {
121
- return this ;
122
- }
123
- if ( ! silent ) {
124
- while ( this . _pending ) {
125
- options = this . _pending ;
126
- this . _pending = false ;
127
- this . trigger ( 'change' , this , options ) ;
123
+ // You might be wondering why there's a `while` loop here. Changes can
124
+ // be recursively nested within `"change"` events.
125
+ if ( changing ) {
126
+ return this ;
127
+ }
128
+ if ( ! silent ) {
129
+ while ( this . _pending ) {
130
+ options = this . _pending ;
131
+ this . _pending = false ;
132
+ this . trigger ( 'change' , this , options ) ;
133
+ }
128
134
}
135
+ } finally {
136
+ this . _pending = false ;
137
+ this . _changing = false ;
129
138
}
130
- this . _pending = false ;
131
- this . _changing = false ;
132
139
return this ;
133
140
}
0 commit comments