/
ko.linq.js
69 lines (62 loc) · 1.91 KB
/
ko.linq.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*global define:false*/
(function (window, undefined) {
"use strict";
(function (factory) {
if (typeof define === "function" && define.amd) { // AMD
define("ko.linq", ["knockout", "linqjs"], factory);
}
else {
window.ko.linq = factory(window.ko, window.Enumerable);
}
}(function (ko, Enumerable) {
/**
* Creates a new instance of LinqBuilder
*
* @constructor
* @param {ko.observableArray} source The source observableArray
* @return A new instance of LinqBuilder
*/
function LinqBuilder(source) {
this.source = source;
this.enumerable = new Enumerable(function () {
return Enumerable.From(source()).GetEnumerator();
});
}
/**
* Creates an instance of ko.computed that will return the observable
* results of the built LINQ query.
*
* @return {ko.computed} An observable result of the query.
*/
LinqBuilder.prototype.toComputed = function () {
var self = this;
return ko.computed(function () {
return self.enumerable.ToArray();
});
};
function createMediator(property) {
LinqBuilder.prototype[toCamelCase(property)] = function () {
this.enumerable = Enumerable.prototype[property].apply(this.enumerable, arguments);
return this;
};
}
for (var property in Enumerable.prototype) {
if (!Enumerable.prototype[property].apply) { continue; }
createMediator(property);
}
var firstCharacter = /^.{1}/;
function toCamelCase(property) {
return property.replace(firstCharacter, function (char) {
return char.toLowerCase();
});
}
/**
* Start a new linq query based on the observablearray
*
* @return {LinqBuilder} A new LinqBuilder instance
*/
ko.observableArray.fn.linq = function () {
return new LinqBuilder(this);
};
}));
}(window));