33var Emitter = require ( './emitter' ) ,
44 utils = require ( './utils' ) ,
55 // cache methods
6- typeOf = utils . typeOf ,
76 def = utils . defProtected ,
7+ isObject = utils . isObject ,
8+ isArray = Array . isArray ,
89 hasOwn = ( { } ) . hasOwnProperty ,
910 oDef = Object . defineProperty ,
1011 slice = [ ] . slice ,
11- // types
12- OBJECT = 'Object' ,
13- ARRAY = 'Array' ,
1412 // fix for IE + __proto__ problem
1513 // define methods as inenumerable if __proto__ is present,
1614 // otherwise enumerable so we can loop through and manually
1715 // attach to array instances
18- hasProto = ( { } ) . __proto__ ,
19- // lazy load
20- ViewModel
16+ hasProto = ( { } ) . __proto__
2117
2218// Array Mutation Handlers & Augmentations ------------------------------------
2319
@@ -157,9 +153,7 @@ def(ObjProxy, '$delete', function (key) {
157153 * Check if a value is watchable
158154 */
159155function isWatchable ( obj ) {
160- ViewModel = ViewModel || require ( './viewmodel' )
161- var type = typeOf ( obj )
162- return ( type === OBJECT || type === ARRAY ) && ! ( obj instanceof ViewModel )
156+ return typeof obj === 'object' && obj && ! obj . $compiler
163157}
164158
165159/**
@@ -196,11 +190,10 @@ function propagateChange (obj) {
196190 * Watch target based on its type
197191 */
198192function watch ( obj ) {
199- var type = typeOf ( obj )
200- if ( type === OBJECT ) {
201- watchObject ( obj )
202- } else if ( type === ARRAY ) {
193+ if ( isArray ( obj ) ) {
203194 watchArray ( obj )
195+ } else {
196+ watchObject ( obj )
204197 }
205198}
206199
@@ -279,7 +272,7 @@ function convertKey (obj, key) {
279272 function init ( val , propagate ) {
280273 values [ key ] = val
281274 emitter . emit ( 'set' , key , val , propagate )
282- if ( Array . isArray ( val ) ) {
275+ if ( isArray ( val ) ) {
283276 emitter . emit ( 'set' , key + '.length' , val . length , propagate )
284277 }
285278 observe ( val , key , emitter )
@@ -293,11 +286,11 @@ function convertKey (obj, key) {
293286 * all of its properties.
294287 */
295288function emitSet ( obj ) {
296- var type = typeOf ( obj ) ,
297- emitter = obj && obj . __emitter__
298- if ( type === ARRAY ) {
289+ var emitter = obj && obj . __emitter__
290+ if ( ! emitter ) return
291+ if ( isArray ( obj ) ) {
299292 emitter . emit ( 'set' , 'length' , obj . length )
300- } else if ( type === OBJECT ) {
293+ } else {
301294 var key , val
302295 for ( key in obj ) {
303296 val = obj [ key ]
@@ -314,19 +307,18 @@ function emitSet (obj) {
314307 * emit a set event with undefined value.
315308 */
316309function copyPaths ( newObj , oldObj ) {
317- if ( typeOf ( oldObj ) !== OBJECT || typeOf ( newObj ) !== OBJECT ) {
310+ if ( ! isObject ( newObj ) || ! isObject ( oldObj ) ) {
318311 return
319312 }
320- var path , type , oldVal , newVal
313+ var path , oldVal , newVal
321314 for ( path in oldObj ) {
322315 if ( ! ( hasOwn . call ( newObj , path ) ) ) {
323316 oldVal = oldObj [ path ]
324- type = typeOf ( oldVal )
325- if ( type === OBJECT ) {
317+ if ( isArray ( oldVal ) ) {
318+ newObj [ path ] = [ ]
319+ } else if ( isObject ( oldVal ) ) {
326320 newVal = newObj [ path ] = { }
327321 copyPaths ( newVal , oldVal )
328- } else if ( type === ARRAY ) {
329- newObj [ path ] = [ ]
330322 } else {
331323 newObj [ path ] = undefined
332324 }
@@ -348,7 +340,7 @@ function ensurePath (obj, key) {
348340 }
349341 obj = obj [ sec ]
350342 }
351- if ( typeOf ( obj ) === OBJECT ) {
343+ if ( isObject ( obj ) ) {
352344 sec = path [ i ]
353345 if ( ! ( hasOwn . call ( obj , sec ) ) ) {
354346 obj [ sec ] = undefined
0 commit comments