2
Vote

Using linq.js with Knockout.js causes weird dictionary issue

description

Given an object used as a plain key value dictionary, Enumerable.From will fail to consider any keys whose values are an observableArray. For example:

Enumerable.From({ 'example': ko.observableArray() }).ToArray()
=> []

comments

Igorbek wrote Jan 20, 2013 at 9:42 PM

ver 3.0.3-Beta4, same problem.

neuecc wrote Jan 22, 2013 at 6:48 AM

Thanks for report.

ko.observableArray is strange, ko.observableArray is function.
Enumerable.from filter function value.
Therefore result is empty.

Please make an exclusive method.
Enumerable.fromObject = function (obj) {
    return Enumerable.Utils.createEnumerable(function () {
        var array = [];
        var index = 0;

        return Enumerable.Utils.createEnumerator(
            function () {
                for (var key in obj) {
                    var value = obj[key];
                    if (Object.prototype.hasOwnProperty.call(obj, key)) {
                        array.push({ key: key, value: value });
                    }
                }
            },
            function () {
                return (index < array.length)
                    ? this.yieldReturn(array[index++])
                    : this.yieldBreak();
            },
            function () { });
    })
};
usage
var xs = Enumerable.fromObject({ 'example': ko.observableArray() }).toArray();