import * as utils from './utils' ; import { ModelSetOptions , ObjectHash } from 'backbone' ; import { JSONExt } from '@lumino/coreutils' ; export function set ( key , val , options ) { if ( key == null ) { return this ; } let attrs ; if ( JSONExt . isObject ( key ) ) { attrs = key ; options = val ; } else { ( attrs = { } as ObjectHash ) [ key ] = val ; } options || ( options = { } ) ; if ( ! this . _validate ( attrs , options ) ) { return false ; } const unset = options . unset ; const silent = options . silent ; const changes = [ ] ; const changing = this . _changing ; this . _changing = true ; if ( ! changing ) { this . _previousAttributes = { ... this . attributes } ; this . changed = { } ; } const current = this . attributes ; const changed = this . changed ; const prev = this . _previousAttributes ; for ( const attr in attrs ) { val = attrs [ attr ] ; if ( ! utils . isEqual ( current [ attr ] , val ) ) { changes . push ( attr ) ; } if ( ! utils . isEqual ( prev [ attr ] , val ) ) { changed [ attr ] = val ; } else { delete changed [ attr ] ; } unset ? delete current [ attr ] : ( current [ attr ] = val ) ; } this . id = this . get ( this . idAttribute ) ; if ( ! silent ) { if ( changes . length ) { this . _pending = options ; } for ( let i = 0 ; i < changes . length ; i ++ ) { this . trigger ( 'change:' + changes [ i ] , this , current [ changes [ i ] ] , options ) ; } } if ( changing ) { return this ; } if ( ! silent ) { while ( this . _pending ) { options = this . _pending ; this . _pending = false ; this . trigger ( 'change' , this , options ) ; } } this . _pending = false ; this . _changing = false ; return this ; }