forked from ericmbarnard/Knockout-ToolKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknockout.toolkit.js
92 lines (71 loc) · 3.04 KB
/
knockout.toolkit.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/// <reference path="libs/jquery-1.7.2.js" />
/// <reference path="libs/knockout-2.1.0.debug.js" />
/*
=======================================================================
Description: A Toolkit for KnockoutJS
Author: Eric M. Barnard (https://github.com/ericmbarnard)
License: MIT (http://www.opensource.org/licenses/mit-license.php)
=======================================================================
*/
;(function (ko, undefined) {
if (!ko) {
throw Error("You must load KnockoutJS before using Knockout Toolkit!");
}
// some quick tests to see what is supported
var hasJQuery = (window.jQuery ? true : false);
// in case jQuery is being used noConflict
var $ = (hasJQuery ? window.jQuery : null);
/*========================= Utils ============================*/
// an enhanced version of 'ko.utils.extend'
// this unwraps observables to use their values during extending
// and will also 'set' observable values on the target
ko.utils['extendObservable'] = function ( target, source ) {
var prop,
srcVal,
isObservable = false;
for ( prop in source ) {
if ( !source.hasOwnProperty( prop ) ) {
continue;
}
if ( ko.isWriteableObservable( source[prop]) ) {
isObservable = true;
srcVal = source[prop]();
} else if ( typeof ( source[prop] ) !== 'function' ) {
srcVal = source[prop];
}
if ( ko.isWriteableObservable(target[prop]) ) {
target[prop]( srcVal );
} else if ( target[prop] === null || target[prop] === undefined ) {
target[prop] = isObservable ? ko.observable( srcVal ) : srcVal;
} else if ( typeof ( target[prop] ) !== 'function' ) {
target[prop] = srcVal;
}
// resets for the loop
isObservable = false;
srcVal = null;
}
};
// an enhanced version of 'ko.utils.extend' that
// ensures no memory pointers are given to the copied
// object.
// target -> empty instance to be populated
// source -> hydrated object to retrieve values from
ko.utils['extendClone'] = function ( target, source ) {
var json = ko.toJSON( source ),
js = ko.utils.parseJson( json );
return ko.utils.extendObservable( target, js );
};
/*========================= Extenders ============================*/
// TODO: add extender functions here
/*========================= Binding Handlers ============================*/
// invisible -> the inverse of 'visible'
ko.bindingHandlers['invisible'] = {
update: function (element, valueAccessor) {
var newValueAccessor = function () {
// just return the opposite of the visible flag!
return !ko.utils.unwrapObservable(valueAccessor());
};
return ko.bindingHandlers.visible.update(element, newValueAccessor);
}
};
}(window.ko));