1
1
import utils from './utils'
2
2
import Component from './Component'
3
3
import Query from './Query'
4
+ import Record from './Record'
4
5
import Index from '../lib/mindex/index'
5
6
7
+ const { noValidatePath } = Record
8
+
6
9
const DOMAIN = 'Collection'
7
10
8
11
const COLLECTION_DEFAULTS = {
@@ -212,7 +215,7 @@ export default Component.extend({
212
215
* @param {(Object|Object[]|Record|Record[]) } data The record or records to insert.
213
216
* @param {Object } [opts] Configuration options.
214
217
* @param {boolean } [opts.commitOnMerge=true] See {@link Collection#commitOnMerge}.
215
- * @param {boolean } [opts.noValidate] See {@link Mapper #noValidate}.
218
+ * @param {boolean } [opts.noValidate] See {@link Record #noValidate}.
216
219
* @param {string } [opts.onConflict] See {@link Collection#onConflict}.
217
220
* @returns {(Object|Object[]|Record|Record[]) } The added record or records.
218
221
*/
@@ -254,11 +257,13 @@ export default Component.extend({
254
257
// Here, the currently visited record corresponds to a record already
255
258
// in the collection, so we need to merge them
256
259
const onConflict = opts . onConflict || this . onConflict
257
- let restoreNoValidate
258
- // `noValidate` might have been overriden in find/findAll. respect that
259
- if ( opts . noValidate !== undefined ) {
260
- restoreNoValidate = existing . _set . bind ( existing , 'noValidate' , existing . _get ( 'noValidate' ) )
261
- existing . _set ( 'noValidate' , opts . noValidate )
260
+ if ( onConflict !== 'merge' && onConflict !== 'replace' ) {
261
+ throw utils . err ( `${ DOMAIN } #add` , 'opts.onConflict' ) ( 400 , 'one of (merge, replace)' , onConflict , true )
262
+ }
263
+ const existingNoValidate = existing . _get ( noValidatePath )
264
+ if ( opts . noValidate ) {
265
+ // Disable validation
266
+ existing . _set ( noValidatePath , true )
262
267
}
263
268
if ( onConflict === 'merge' ) {
264
269
utils . deepMixIn ( existing , record )
@@ -269,13 +274,11 @@ export default Component.extend({
269
274
}
270
275
} )
271
276
existing . set ( record )
272
- } else {
273
- // restore prev `noValidate` before throwing if it was overriden for the record
274
- utils . isFunction ( restoreNoValidate ) && restoreNoValidate ( )
275
- throw utils . err ( `${ DOMAIN } #add` , 'opts.onConflict' ) ( 400 , 'one of (merge, replace)' , onConflict , true )
276
277
}
277
- // restore prev `noValidate` value if it was overriden for the record
278
- utils . isFunction ( restoreNoValidate ) && restoreNoValidate ( )
278
+ if ( opts . noValidate ) {
279
+ // Restore previous `noValidate` value
280
+ existing . _set ( noValidatePath , existingNoValidate )
281
+ }
279
282
record = existing
280
283
if ( opts . commitOnMerge && utils . isFunction ( record . commit ) ) {
281
284
record . commit ( )
0 commit comments