angularjs filter (not working)

前端 未结 7 1344
灰色年华
灰色年华 2020-12-25 14:56

The following HTML, Javascript and JSON render correctly, but the filter does not work at all. What are we doing wrong?



        
7条回答
  •  天命终不由人
    2020-12-25 15:40

    What Is Worth Mentioning...

    Given, this is rather a corner case, but I thought I'd share.

    You've a data-structure; it needs to be a collection, but it requires very SOLID fundamentals and its own methods due to expected growth, complexity, or other constraints. So you decide to prototype this object as an array.

    var OrdersDataModel = function OrdersDataModel(orders) {
        var thus = this;
        var orders = orders || [];
    
        function loadData(data) {
            this.splice.apply(this, [0, this.length].concat(data || orders));
            return this;
        }
        function serialize() {
            var serialized = JSON.stringify(this);
            return serialized;
        }
        function marshalData() {
            var marshalled = JSON.parse(this.serialize());
            return marshalled;
        }
    
        // export precepts
        this.load = loadData;
        this.serialize = serialize;
        this.marshalData = marshalData;
    
        return this;
    };
    OrdersDataModel.prototype = new Array();
    

    This may suit your needs and perform well in your ng-repeat -- until comes the need to filter your "array":

    
    

    Your filter will fail (probably simply due to type-checks), and you'll wast a little time perhaps.

    In my case, it can be easily remedied by modding my serialize method from

    var serialized = JSON.stringify(this);
    

    -- to --

    var serialized = JSON.stringify(this.slice(0));
    

    Though, a much better way to accomplish my goal would be to decorate an array using either the Decorator Pattern or Constructor-Hijacking:

    Instead of:

    OrdersDataModel.prototype = new Array();
    ...
    this.orders = new OrdersDataModel();
    this.orders.load(serverData);
    

    -- use --

    this.orders = OrdersDataModel.apply([]);
    this.orders.load(serverData);
    

    A lot of this may seem a little extraneous, but I expect this (and its encompassing module/controller) to expand quickly, so I've stuck as much to SOLID as possible to keep it air-tight.

    Hope this saves you some time!

提交回复
热议问题