Bläddra i källkod

warn v-for filters that do not return Arrays

Evan You 10 år sedan
förälder
incheckning
00568ce251
2 ändrade filer med 22 tillägg och 0 borttagningar
  1. 6 0
      src/directives/public/for.js
  2. 16 0
      test/unit/specs/directives/public/for/for_spec.js

+ 6 - 0
src/directives/public/for.js

@@ -61,6 +61,12 @@ module.exports = {
   },
   },
 
 
   update: function (data) {
   update: function (data) {
+    if (process.env.NODE_ENV !== 'production' && !_.isArray(data)) {
+      _.warn(
+        'v-for pre-converts Objects into Arrays, and ' +
+        'v-for filters should always return Arrays.'
+      )
+    }
     this.diff(data)
     this.diff(data)
     this.updateRef()
     this.updateRef()
     this.updateModel()
     this.updateModel()

+ 16 - 0
test/unit/specs/directives/public/for/for_spec.js

@@ -660,6 +660,22 @@ if (_.inBrowser) {
       expect(hasWarned(_, 'It seems you are using two-way binding')).toBe(true)
       expect(hasWarned(_, 'It seems you are using two-way binding')).toBe(true)
     })
     })
 
 
+    it('warn filters that return non-Array values', function () {
+      new Vue({
+        el: el,
+        template: '<div v-for="item in items | test"></div>',
+        data: {
+          items: []
+        },
+        filters: {
+          test: function (val) {
+            return {}
+          }
+        }
+      })
+      expect(hasWarned(_, 'should always return Arrays')).toBe(true)
+    })
+
     it('nested track by', function (done) {
     it('nested track by', function (done) {
       var vm = new Vue({
       var vm = new Vue({
         el: el,
         el: el,