The following HTML, Javascript and JSON render correctly, but the filter does not work at all. What are we doing wrong?
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!