Skip to content

Commit 76b1734

Browse files
author
Vlad Balin
committed
Merge remote-tracking branch 'refs/remotes/origin/develop'
2 parents 94759ce + 2b69b89 commit 76b1734

File tree

7 files changed

+88
-167
lines changed

7 files changed

+88
-167
lines changed

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,22 @@ Event subscription is managed automatically. No props passed - no problems.
180180
## Data binding
181181

182182
`nestedreact` supports data binding links compatible with standard React's `valueLink`.
183-
Links are "live" in a sense that they always point to actual value based on current model or collection state.
184-
It doesn't break anything in React, rather extends possible use cases.
185183

186-
- `var link = model.getLink( 'attr' )` creates link for model attribute.
187-
- `var link = collection.getLink( model )` creates boolean link, toggling model in collection. True if model is contained in collection, assignments will add/remove given model. Useful for checkboxes.
184+
Create link for object property of Model, Collection, and every object type created with Object.extend().
188185

189-
### Value access methods
186+
`var link = object.getLink( 'attr' )`
190187

191-
In addition to standard members `link.requestChange( x )` and `link.value`, links supports all popular property access styles:
188+
Create boolean link, toggling model in collection. True if model is contained in collection, assignments will add/remove given model. Useful for checkboxes.
189+
`var link = collection.hasLink( model )`
192190

193-
- jQuery property style: setter `link.val( x )`, getter `link.val()`
194-
- Backbone style: setter `link.set( x )`, getter `link.get()`
195-
- plain assugnments style: setter `link.value = x`, getter `link.value`
196-
- `link.toggle()` is a shortcut for `link.requestChange( !link.value )`
191+
Here's a brief reference for links API. Consult [Guide to Data Binding Use Cases](/example/databinding.md) to understand how to use it.
197192

198-
Most efficient way to work with link is using `link.val()` function, that's how its internally implemented. `val` function is bound, and can be passed around safely.
193+
### Value access methods
199194

200-
Here's a brief reference for liks API. Consult [Guide to Data Binding Use Cases](/example/databinding.md) to understand how to use it.
195+
In addition to standard members `link.requestChange( x )` and `link.value`, there are pair of shortcuts available:
201196

197+
- `link.set( x )`, which is a shortcut for `this.requestChange( x )`
198+
- `link.toggle()` is a shortcut for `link.requestChange( !link.value )`
202199

203200
### Link transformations
204201

example/databinding.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Standard `<input/>` will work. Custom Checkbox component might be implemented li
1818

1919
```javascript
2020
const Checkbox = ({ className = 'checkbox', checkedLink }) => (
21-
<div className={ className + ( checkedLink.val() ? ' selected' : '' ) }
21+
<div className={ className + ( checkedLink.value ? ' selected' : '' ) }
2222
onClick = { checkedLink.update( x => !x ) }
2323
/>
2424
);
@@ -113,7 +113,7 @@ with one difference in click handler:
113113

114114
```javascript
115115
const Radio = ({ className = 'radio', checkedLink }) => (
116-
<div className={ className + ( checkedLink.val() ? ' selected' : '' ) }
116+
<div className={ className + ( checkedLink.value ? ' selected' : '' ) }
117117
onClick = { checkedLink.update( () => true ) }
118118
/>
119119
);
@@ -155,7 +155,7 @@ with validation and appearance.
155155
```javascript
156156
const Input = ({ valueLink, ...props }) => (
157157
<div className='wrapping'
158-
<input {...props} value={ valueLink.val() } onChange={ e => valueLink.val( e.target.value ) }/>
158+
<input {...props} value={ valueLink.value } onChange={ e => valueLink.set( e.target.value ) }/>
159159
</div>
160160
);
161161
```

nestedreact.js

Lines changed: 37 additions & 75 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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"nestedreact.js.map"
3030
],
3131
"license": "MIT",
32-
"version": "0.3.0",
32+
"version": "0.4.0",
3333
"scripts": {
3434
"build": "./node_modules/.bin/webpack",
3535
"watch": "./node_modules/.bin/webpack --watch"

src/main.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,23 @@ var ValueLink = require( './value-link' );
3737
var Link = Nested.Link = ValueLink.Link;
3838
Nested.link = ValueLink.link;
3939

40-
var ModelProto = Nested.Model.prototype;
40+
var ClassProto = Nested.Class.prototype,
41+
ModelProto = Nested.Model.prototype,
42+
CollectionProto = Nested.Collection.prototype;
4143

42-
ModelProto.getLink = function( attr ){
44+
ClassProto.getLink = ModelProto.getLink = CollectionProto.getLink = function( attr ){
4345
var model = this;
4446

45-
return new Link( function( x ){
46-
if( arguments.length ){
47-
model[ attr ] = x;
48-
}
49-
50-
return model[ attr ];
47+
return new Link( model[ attr ], function( x ){
48+
model[ attr ] = x;
5149
});
5250
};
5351

54-
var CollectionProto = Nested.Collection.prototype;
55-
56-
CollectionProto.getLink = function( model ){
52+
CollectionProto.hasLink = function( model ){
5753
var collection = this;
5854

59-
return new Link( function( x ){
60-
var prev = Boolean( collection.get( model ) );
61-
62-
if( arguments.length ){
63-
var next = Boolean( x );
64-
if( prev !== next ){
65-
collection.toggle( model, x );
66-
return next;
67-
}
68-
}
69-
70-
return prev;
55+
return new Link( Boolean( collection.get( model ) ), function( x ){
56+
var next = Boolean( x );
57+
this.value === next || collection.toggle( model, next );
7158
});
7259
};

0 commit comments

Comments
 (0)