@@ -61,12 +61,6 @@ module.exports = {
6161 } ,
6262
6363 update : function ( data ) {
64- if ( process . env . NODE_ENV !== 'production' && ! _ . isArray ( data ) ) {
65- _ . warn (
66- 'v-for pre-converts Objects into Arrays, and ' +
67- 'v-for filters should always return Arrays.'
68- )
69- }
7064 this . diff ( data )
7165 this . updateRef ( )
7266 this . updateModel ( )
@@ -87,25 +81,31 @@ module.exports = {
8781 */
8882
8983 diff : function ( data ) {
84+ // check if the Array was converted from an Object
85+ var item = data [ 0 ]
86+ var convertedFromObject = this . fromObject =
87+ isObject ( item ) &&
88+ item . hasOwnProperty ( '$key' ) &&
89+ item . hasOwnProperty ( '$value' )
90+
9091 var idKey = this . idKey
91- var converted = this . converted
9292 var oldFrags = this . frags
9393 var frags = this . frags = new Array ( data . length )
9494 var alias = this . alias
9595 var start = this . start
9696 var end = this . end
9797 var inDoc = _ . inDoc ( start )
9898 var init = ! oldFrags
99- var i , l , frag , item , key , value , primitive
99+ var i , l , frag , key , value , primitive
100100
101101 // First pass, go through the new Array and fill up
102102 // the new frags array. If a piece of data has a cached
103103 // instance for it, we reuse it. Otherwise build a new
104104 // instance.
105105 for ( i = 0 , l = data . length ; i < l ; i ++ ) {
106106 item = data [ i ]
107- key = converted ? item . $key : null
108- value = converted ? item . $value : item
107+ key = convertedFromObject ? item . $key : null
108+ value = convertedFromObject ? item . $value : item
109109 primitive = ! isObject ( value )
110110 frag = ! init && this . getCachedFrag ( value , i , key )
111111 if ( frag ) { // reusable fragment
@@ -126,7 +126,7 @@ module.exports = {
126126 }
127127 // update data for track-by, object repeat &
128128 // primitive values.
129- if ( idKey || converted || primitive ) {
129+ if ( idKey || convertedFromObject || primitive ) {
130130 frag . scope [ alias ] = value
131131 }
132132 } else { // new isntance
@@ -230,7 +230,7 @@ module.exports = {
230230 if ( ! ref ) return
231231 var hash = ( this . _scope || this . vm ) . $refs
232232 var refs
233- if ( ! this . converted ) {
233+ if ( ! this . fromObject ) {
234234 refs = this . frags . map ( findVmFromFrag )
235235 } else {
236236 refs = { }
@@ -458,29 +458,28 @@ module.exports = {
458458
459459 /**
460460 * Pre-process the value before piping it through the
461- * filters, and convert non-Array objects to arrays.
462- *
463- * This function will be bound to this directive instance
464- * and passed into the watcher.
465- *
466- * @param {* } value
467- * @return {Array }
468- * @private
461+ * filters. This is passed to and called by the watcher.
469462 */
470463
471464 _preProcess : function ( value ) {
472465 // regardless of type, store the un-filtered raw value.
473466 this . rawValue = value
474- var type = this . rawType = typeof value
475- if ( ! _ . isPlainObject ( value ) ) {
476- this . converted = false
477- if ( type === 'number' ) {
478- value = range ( value )
479- } else if ( type === 'string' ) {
480- value = _ . toArray ( value )
481- }
482- return value || [ ]
483- } else {
467+ return value
468+ } ,
469+
470+ /**
471+ * Post-process the value after it has been piped through
472+ * the filters. This is passed to and called by the watcher.
473+ *
474+ * It is necessary for this to be called during the
475+ * wathcer's dependency collection phase because we want
476+ * the v-for to update when the source Object is mutated.
477+ */
478+
479+ _postProcess : function ( value ) {
480+ if ( _ . isArray ( value ) ) {
481+ return value
482+ } else if ( _ . isPlainObject ( value ) ) {
484483 // convert plain object to array.
485484 var keys = Object . keys ( value )
486485 var i = keys . length
@@ -493,8 +492,15 @@ module.exports = {
493492 $value : value [ key ]
494493 }
495494 }
496- this . converted = true
497495 return res
496+ } else {
497+ var type = typeof value
498+ if ( type === 'number' ) {
499+ value = range ( value )
500+ } else if ( type === 'string' ) {
501+ value = _ . toArray ( value )
502+ }
503+ return value || [ ]
498504 }
499505 } ,
500506
0 commit comments