解析 + Angular orderBy 问题

Parse + Angular orderBy Issue

我在 Angular 中有一个 table 的 ng-repeat。数据来自 Parse。

    <tr dir-paginate="ticket in tickets | orderBy:sortType:sortReverse | itemsPerPage: 10">
        <td><input type="checkbox" class="checkthis" /></td>
        <td>{{ ticket.id }}</td>
        <td>${{ ticket.get("total").toFixed(2) }}</td>
        <td>${{ ticket.get("tax").toFixed(2) }}</td>
        <td>{{ ticket.get("paymentType") }}</td>
        <td>{{ ticket.createdAt | date: 'short' }}</td>
    </tr>

当我按 'id' 或 'createdAt' 排序时,排序工作正常。我该如何按总计、税金或支付类型进行订购?

解析对象的属性是通过 get() 访问的,我认为 orderBy: 过滤器取决于本地 getter.

因此,在 angular 中,一种方法是创建一个扩展 backbone 对象并提供本机 getter 和设置器的服务:

(function() {
    'use strict';
    angular.module('myApp.services').factory('MyClass', f);

    function f() {

        var MyClass = Parse.Object.extend("MyClass", {
            // instance methods

            // manually built getter like this
            attributeA : function() {
                return this.get("attributeA");
            },

        }, {
            // class methods    
        });

        // or code-built getters/setters like this
        _.each(["attributeB", "attributeC"], function(p) {
            Object.defineProperty(MyClass.prototype, p, {
                get: function() {return this.get(p);},
                set: function(aValue) {this.set(p, aValue);}
            });
        });
        return MyClass;
    }
})();

无论如何,这可能是个好习惯,即使您不需要 orderBy 的属性。例如,服务 class 是放置承诺返回查询的好地方。

如果您的数据来自 Parse JavaScript SDK,您会收到它们作为 Parse 对象,您可以通过以下方式将其转换为更适合 AngularJS 的普通对象.toJSON() 方法:

plainObject = parseObject.toJSON();