Skip to content

Commit 5184702

Browse files
author
Vlad Balin
committed
Added support for databinding
- Nested.valueLink( '^props.valueLink' ) to create fake state attribute - Model.lget to create valueLinks - Model.fset to create click handlers - Collection.lhas creates boolean link - Collection ftoggle( model ) creates click handler - valueLink.set( val ) to update link - valueLink.fset( val ) to create click handler - valueLink.leql( val ) creates boolean link - valueLink.lhas( val ) creates boolean link for array
1 parent 7dadd95 commit 5184702

File tree

4 files changed

+296
-1
lines changed

4 files changed

+296
-1
lines changed

nestedreact.js

Lines changed: 150 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nestedreact.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,27 @@ Object.defineProperties( BaseComponent, {
3030
$el : { get : function(){ return $( this.el ); } },
3131
$ : { value : function( sel ){ return this.$el.find( sel ); } }
3232
} );
33+
34+
var Link = NestedReact.Link = require( './valuelink' );
35+
Nested.valueLink = Link.valueLink;
36+
37+
var ModelProto = Nested.Model.prototype,
38+
LinkAttr = Link.Attr;
39+
40+
ModelProto.lget = function( name ){ return new LinkAttr( this, name ); };
41+
ModelProto.fset = function( a, b, c ){
42+
var self = this;
43+
return function(){ self.set( a, b, c ); }
44+
};
45+
46+
var CollectionProto = Nested.Collection.prototype,
47+
LinkHas = Link.CollectionHas;
48+
49+
CollectionProto.lhas = function( model ){
50+
return new LinkHas( this, model );
51+
};
52+
53+
CollectionProto.ftoggle = function( model, next ){
54+
var self = this;
55+
return function(){ self.toggle( model, next ); }
56+
};

src/valuelink.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
var Nested = require( 'nestedtypes' );
2+
3+
var Value = exports.Value = Object.extend( {
4+
value : void 0,
5+
requestChanges : function( val ){ throw new ReferenceError(); },
6+
7+
set : function( val ){ this.requestChanges( val ); },
8+
fset : function( val ){
9+
var link = this;
10+
return function(){ link.requestChanges( val ); }
11+
}
12+
} );
13+
14+
exports.Attr = Value.extend( {
15+
constructor : function( model, attr ){
16+
this.value = model[ attr ];
17+
this.requestChanges = function( val ){
18+
model[ attr ] = val;
19+
}
20+
},
21+
22+
// for array links
23+
lhas : function( value ){
24+
return new ArrayHas( this, value );
25+
},
26+
27+
leql : function( value ){
28+
return new ValueEql( this, value );
29+
}
30+
} );
31+
32+
var Bool = exports.Bool = Value.extend( {
33+
toggle : function(){ this.requestChanges( !this.value ); },
34+
35+
ftoggle : function(){
36+
var link = this;
37+
return function(){ link.requestChanges( !link.value ) };
38+
}
39+
} );
40+
41+
var ValueEql = exports.ValueEql = Bool.extend( {
42+
constructor : function( link, asTrue ){
43+
this.value = link.value === asTrue;
44+
this.requestChanges = function( val ){
45+
link.requestChanges( val ? asTrue : null );
46+
}
47+
}
48+
} );
49+
50+
var ArrayHas = exports.ArrayHas = Bool.extend( {
51+
constructor : function( link, element ){
52+
var value = Boolean( contains( link.value, element ) );
53+
this.value = value;
54+
55+
this.requestChanges = function( next ){
56+
if( value !== Boolean( next ) ){
57+
var prev = link.value;
58+
link.requestChanges( next ? prev.concat( element ) : without( prev, element ) );
59+
}
60+
};
61+
}
62+
} );
63+
64+
exports.CollectionHas = Bool.extend( {
65+
constructor : function( collection, model ){
66+
this.value = Boolean( collection.get( model ) );
67+
this.requestChanges = function( val ){ collection.toggle( model, val ); }
68+
}
69+
} );
70+
71+
exports.valueLink = function( reference ){
72+
var getMaster = Nested.parseReference( reference );
73+
74+
function setLink( value ){
75+
var link = getMaster.call( this );
76+
link && link.requestChanges( value );
77+
}
78+
79+
function getLink(){
80+
var link = getMaster.call( this );
81+
return link && link.value;
82+
}
83+
84+
var LinkAttribute = Nested.attribute.Type.extend( {
85+
createPropertySpec : function(){
86+
return {
87+
// call to optimized set function for single argument. Doesn't work for backbone types.
88+
set : setLink,
89+
90+
// attach get hook to the getter function, if present
91+
get : getLink
92+
}
93+
},
94+
95+
set : setLink
96+
} );
97+
98+
var options = Nested.attribute( { toJSON : false } );
99+
options.Attribute = LinkAttribute;
100+
return options;
101+
};
102+
103+
// private array helpers
104+
function contains( arr, el ){
105+
for( var i = 0; i < arr.length; i++ ){
106+
if( arr[ i ] === el ) return true;
107+
}
108+
109+
return false;
110+
}
111+
112+
function without( arr, el ){
113+
var res = [];
114+
115+
for( var i = 0; i < arr.length; i++ ){
116+
var current = arr[ i ];
117+
current === el || res.push( current );
118+
}
119+
120+
return res;
121+
}

0 commit comments

Comments
 (0)