@@ -2,87 +2,89 @@ import inspect from './inspect';
22
33var noop = function ( ) { } ;
44
5- function BoundElement ( config ) {
6- this . element = inspect . isString ( config . element ) ? document . querySelector ( config . element ) : config . element ;
7- this . dataSource = config . dataSource ;
8- this . path = config . path ;
9- this . to = config . to || 'html' ;
10- this . _callback = config . callback || noop ;
11- this . _bound = true ;
12- this . _boundHandler = this . update . bind ( this ) ;
13- this . dataSource . bindHandler ( this . _boundHandler ) ;
14-
15- this . onBind = config . onBind || noop ;
16- this . onUpdate = config . onUpdate || noop ;
17- this . onUnbind = config . onUnbind || noop ;
18-
19- this . onBind . call ( this . element ) ;
20-
21- if ( ! config . disableUpdate ) {
22- this . update ( ) ;
5+ export default function initModule ( renderer , dataSource ) {
6+ function BoundElement ( config ) {
7+ this . element = inspect . isString ( config . to ) ? document . querySelectorAll ( config . to ) : config . to ;
8+ this . multiple = config . multiple || ( this . element instanceof NodeList && this . element . length > 1 ) ;
9+
10+ if ( ! this . multiple ) {
11+ this . element = this . element [ 0 ] ;
12+ }
13+
14+ this . dataSource = config . dataSource ;
15+ this . path = config . path ;
16+ this . as = config . as || 'text' ;
17+ this . _bound = true ;
18+ this . _boundHandler = this . update . bind ( this ) ;
19+ this . dataSource . bindHandler ( this . _boundHandler ) ;
20+
21+ this . onBind = config . onBind || noop ;
22+ this . onUpdate = config . onUpdate || noop ;
23+ this . onUnbind = config . onUnbind || noop ;
24+ this . onAfterRender = config . onAfterRender || noop ;
25+
26+ this . onBind . call ( this . element ) ;
27+
28+ if ( ! config . disableUpdate ) {
29+ this . update ( ) ;
30+ }
31+ }
32+
33+ BoundElement . prototype . getValue = getValue ;
34+ BoundElement . prototype . setValue = setValue ;
35+ BoundElement . prototype . run = run ;
36+ BoundElement . prototype . update = update ;
37+ BoundElement . prototype . unbind = unbind ;
38+
39+ return function bind ( config ) {
40+ if ( Array . isArray ( config ) ) {
41+ return bindArray ( config ) ;
42+ }
43+
44+ return bindSingle ( config ) ;
45+ } ;
46+
47+ function bindArray ( items ) {
48+ var results = [ ] ;
49+ for ( var i = 0 ; i < config . length ; i ++ ) {
50+ results . push ( bindSingle ( config [ i ] ) ) ;
2351 }
24- }
2552
26- BoundElement . prototype . get = getValue ;
27- BoundElement . prototype . set = setValue ;
28- BoundElement . prototype . run = run ;
29- BoundElement . prototype . update = update ;
30- BoundElement . prototype . unbind = unbind ;
31-
32- export default function bind ( config , dataSource ) {
33- if ( Array . isArray ( config ) ) {
34- var results = [ ] ;
35- for ( var i = 0 ; i < config . length ; i ++ ) {
36- var item = config [ i ] ;
37- if ( ! item . dataSource ) {
38- item . dataSource = dataSource ;
39- }
40-
41- results . push ( new BoundElement ( item ) ) ;
42- }
43-
44- return results ;
45- }
53+ return results ;
54+ }
4655
56+ function bindSingle ( config ) {
4757 if ( ! config . dataSource ) {
4858 config . dataSource = dataSource ;
4959 }
5060
5161 return new BoundElement ( config ) ;
52- } ;
53-
54- function getValue ( defaultValue ) {
55- return this . dataSource . get ( this . path , defaultValue ) ;
56- }
57-
58- function setValue ( value ) {
59- if ( ! this . _bound ) {
60- throw new Error ( 'Cannot call setValue of unbound instance.' ) ;
61- }
62- this . dataSource . set ( this . path , value ) ;
63- }
64-
65- function update ( ) {
66- var value = this . get ( ) ;
67-
68- if ( this . to === 'html' ) {
69- this . element . innerHTML = String ( value ) ;
70- } else if ( this . to === 'text' ) {
71- this . element . innerText = String ( value ) ;
72- } else if ( this . to === 'callback' ) {
73- this . _callback . call ( this . element , value ) ;
74- }
75-
76- this . onUpdate . call ( this . element , value ) ;
77- }
78-
79- function unbind ( ) {
80- this . _bound = false ;
81- this . dataSource . unbindHandler ( this . _boundHandler ) ;
82- this . onUnbind . call ( this . element , value ) ;
83- }
84-
85- function run ( callback ) {
86- callback . call ( this , this . getValue ( ) ) ;
87- this . update ( ) ;
62+ }
63+
64+ function getValue ( defaultValue ) {
65+ return this . dataSource . get ( this . path , defaultValue ) ;
66+ }
67+
68+ function setValue ( value ) {
69+ if ( ! this . _bound ) {
70+ throw new Error ( 'Cannot call setValue of unbound instance.' ) ;
71+ }
72+ this . dataSource . set ( this . path , value ) ;
73+ }
74+
75+ function update ( ) {
76+ this . onUpdate . call ( this . element , this . getValue ( ) ) ;
77+ return renderer . render ( this ) ;
78+ }
79+
80+ function unbind ( ) {
81+ this . _bound = false ;
82+ this . dataSource . unbindHandler ( this . _boundHandler ) ;
83+ this . onUnbind . call ( this . element , value ) ;
84+ }
85+
86+ function run ( callback ) {
87+ callback . call ( this , this . getValue ( ) ) ;
88+ this . update ( ) ;
89+ }
8890}
0 commit comments