Explorar o código

fix #721 support sorting by and in orderBy filter

Evan You %!s(int64=11) %!d(string=hai) anos
pai
achega
fdfac339b0

+ 6 - 6
src/directives/repeat.js

@@ -176,13 +176,13 @@ module.exports = {
     // instance.
     for (i = 0, l = data.length; i < l; i++) {
       obj = data[i]
-      raw = converted ? obj.value : obj
+      raw = converted ? obj.$value : obj
       vm = !init && this.getVm(raw)
       if (vm) { // reusable instance
         vm._reused = true
         vm.$index = i // update $index
         if (converted) {
-          vm.$key = obj.key // update $key
+          vm.$key = obj.$key // update $key
         }
         if (idKey) { // swap track by id data
           if (alias) {
@@ -264,9 +264,9 @@ module.exports = {
     var original = data
     var meta = { $index: index }
     if (this.converted) {
-      meta.$key = original.key
+      meta.$key = original.$key
     }
-    var raw = this.converted ? data.value : data
+    var raw = this.converted ? data.$value : data
     var alias = this.arg
     var hasAlias = !isObject(raw) || !isPlainObject(data) || alias
     // wrap the raw data with alias
@@ -478,8 +478,8 @@ function objToArray (obj) {
   while (i--) {
     key = keys[i]
     res[i] = {
-      key: key,
-      value: obj[key]
+      $key: key,
+      $value: obj[key]
     }
   }
   // `this` points to the repeat directive instance

+ 2 - 2
src/filters/array-filters.js

@@ -61,8 +61,8 @@ exports.orderBy = function (arr, sortKey, reverseKey) {
   }
   // sort on a copy to avoid mutating original array
   return arr.slice().sort(function (a, b) {
-    a = Path.get(a, key)
-    b = Path.get(b, key)
+    a = _.isObject(a) ? Path.get(a, key) : a
+    b = _.isObject(b) ? Path.get(b, key) : b
     return a === b ? 0 : a > b ? order : -order
   })
 }

+ 32 - 0
test/unit/specs/directives/repeat_spec.js

@@ -372,6 +372,38 @@ if (_.inBrowser) {
       }
     })
 
+    it('orderBy supporting $key for object repeaters', function (done) {
+      var vm = new Vue({
+        el: el,
+        template: '<div v-repeat="obj | orderBy sortKey">{{$value}}</div>',
+        data: {
+          sortKey: '$key',
+          obj: {
+            c: 1,
+            a: 3,
+            b: 2
+          }
+        }
+      })
+      expect(el.innerHTML).toBe('<div>3</div><div>2</div><div>1</div><!--v-repeat-->')
+      vm.sortKey = '$value'
+      _.nextTick(function () {
+        expect(el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div><!--v-repeat-->')
+        done()
+      })
+    })
+
+    it('orderBy supporting $value for primitive arrays', function () {
+      var vm = new Vue({
+        el: el,
+        template: '<div v-repeat="list | orderBy \'$value\'">{{$value}}</div>',
+        data: {
+          list: [3, 2, 1]
+        }
+      })
+      expect(el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div><!--v-repeat-->')
+    })
+
     it('track by id', function (done) {
 
       assertTrackBy('<div v-repeat="list" track-by="id">{{msg}}</div>', function () {