33 * (c) 2016 Vlad Balin & Volicon, MIT License
44 */
55
6- function Link ( value , set , error ) {
7- this . value = value ;
8- this . requestChange = set || doNothing ;
9- this . validationError = error ;
6+ function Link ( value , set ) {
7+ this . value = value ;
8+ this . set = set || doNothing ;
109}
1110
1211// create link to component's state attribute
1312Link . state = function ( component , attr ) {
1413 return new Link ( component . state [ attr ] , function ( x ) {
15- var nextState = { } ;
14+ var nextState = { } ;
1615 nextState [ attr ] = x ;
1716 component . setState ( nextState ) ;
18- } ) ;
17+ } ) ;
1918} ;
2019
2120module . exports = Link ;
@@ -25,30 +24,46 @@ function doNothing( x ){ }
2524var defaultError = 'Invalid value' ;
2625
2726Link . prototype = {
28- value : null ,
29- validationError : null ,
30- requestChange : doNothing ,
27+ // Core link API
28+ // ---------------------------------------------
29+ value : void 0 ,
30+ set : doNothing ,
3131
32- set : function ( x ) { this . requestChange ( x ) ; } ,
33- toggle : function ( ) { this . requestChange ( ! this . value ) ; } ,
34-
35- // create function which updates the link
32+ // Immediately update the link
3633 update : function ( transform ) {
34+ var nextValue = transform ( link . value ) ;
35+ nextValue === void 0 || link . set ( nextValue ) ;
36+ } ,
37+
38+ // Create action function which will updates the link
39+ action : function ( transform ) {
3740 var link = this ;
38- return function ( ) {
39- var nextValue = transform ( link . value ) ;
40- nextValue === void 0 || link . requestChange ( nextValue ) ;
41- }
41+ return function ( ) { link . update ( transform ) } ;
4242 } ,
4343
44+ // React backward compatibility shim
45+ requestChange : function ( x ) { this . set ( x ) ; } ,
46+
47+ // DEPRECATED: Backward compatibility shim
48+ toggle : function ( ) { this . set ( ! this . value ) ; } ,
49+
50+ // Validation API
51+ // --------------------------------------------------
52+ error : void 0 ,
53+
54+ // DEPRECATED: backward compatibility shim
55+ get validationError ( ) { return this . error } ,
56+
4457 check : function ( whenValid , error ) {
45- if ( ! this . validationError && ! whenValid ( this . value ) ) {
46- this . validationError = error || defaultError ;
58+ if ( ! this . error && ! whenValid ( this . value ) ) {
59+ this . error = error || defaultError ;
4760 }
4861
4962 return this ;
5063 } ,
5164
65+ // Link transformations
66+ // --------------------------------------------------
5267 // create boolean link to enclosed array element
5368 contains : function ( element ) {
5469 var link = this ;
@@ -57,7 +72,7 @@ Link.prototype = {
5772 var next = Boolean ( x ) ;
5873 if ( this . value !== next ) {
5974 var arr = link . value ;
60- link . requestChange ( x ? arr . concat ( element ) : without ( arr , element ) ) ;
75+ link . set ( x ? arr . concat ( element ) : without ( arr , element ) ) ;
6176 }
6277 } ) ;
6378 } ,
@@ -67,7 +82,7 @@ Link.prototype = {
6782 var link = this ;
6883
6984 return new Link ( this . value === asTrue , function ( x ) {
70- link . requestChange ( x ? asTrue : null ) ;
85+ link . set ( x ? asTrue : null ) ;
7186 } ) ;
7287 } ,
7388
@@ -80,7 +95,7 @@ Link.prototype = {
8095 var objOrArr = link . value ;
8196 objOrArr = clone ( objOrArr ) ;
8297 objOrArr [ key ] = x ;
83- link . requestChange ( objOrArr ) ;
98+ link . set ( objOrArr ) ;
8499 }
85100 } ) ;
86101 } ,
@@ -93,9 +108,12 @@ Link.prototype = {
93108
94109 // dummies for compatibility with nestedtypes object model...
95110 constructor : Link ,
96- initialize : function ( value , set , error ) { }
111+ initialize : function ( value , set , error ) { }
97112} ;
98113
114+ // Tools
115+ // ============================================
116+
99117function mapObject ( link , object , fun ) {
100118 var res = [ ] ;
101119
@@ -143,7 +161,11 @@ function clone( objOrArray ){
143161 var proto = objOrArray && Object . getPrototypeOf ( objOrArray ) ;
144162
145163 if ( proto === Array . prototype ) return objOrArray . slice ( ) ;
146- if ( proto === Object . prototype ) return Object . assign ( { } , objOrArray ) ;
164+ if ( proto === Object . prototype ) {
165+ var x = { } ;
166+ for ( var i in objOrArray ) x [ i ] = objOrArray [ i ] ;
167+ return x ;
168+ }
147169
148170 return objOrArray ;
149171}
0 commit comments