Parse + Angular orderBy Issue

I have ng-repeat for a table in Angular. The data comes from 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>

      

When I order "id" or "createdAt" the sort works correctly. How can I sort by general, tax or billing type?

+3


source to share


2 answers


The attributes of the analysis objects are accessed through get()

, and I think the filter orderBy:

depends on having its own receiver.

In angular, therefore, one way is to create a service that extends the base object and provides built-in getters and setters:



(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;
    }
})();

      

This is probably good practice, even if you don't need attributes for orderBy. Service class is a good place to, for example, deliver promises return requests.

+1


source


If your data comes in via the Parse JavaScript SDK, you get it as Parse Objects, which you can convert to simple objects more AngularJS-friendly using the method .toJSON()

:



plainObject = parseObject.toJSON();

      

+1


source







All Articles