Skip to content

Commit 0670356

Browse files
joefeserDave Reed
authored andcommitted
Added comments explaining the restricted scope and added a todo to explain the $.convertFn syntax.
1 parent 5011c6d commit 0670356

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

README.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,41 @@ Adds linkTo, linkFrom, and linkBoth plugins.
99
alert("Change attr '" + ev.attrName + "' from '" +
1010
ev.oldValue + "' to '" + ev.newValue + "'.");
1111
}
12-
12+
1313
$("#el").attrChange(report)
1414
// Change attr 'foo' from 'undefined' to 'bar'
1515
.attr("foo", "bar").
1616
// Change attr 'foo' from 'bar' to 'baz'
1717
.attr("foo", "baz");
18-
19-
// restricted scope
20-
$("#el").attrChange("x", report)
21-
// Change attr 'x' from 'undefined' to '1'
22-
.attr( { x: "1", y: "2" } );
23-
24-
$("#el").attrChange([ "x", "y" ], report)
18+
19+
//restricted scope can be thought of as a filter
20+
//no matter now many attributes are passed in,
21+
//only those exactly matching (===) will be changed
22+
23+
// restricted scope of only one property
24+
$("#el").attrChange("x", report)
25+
// Change attr 'x' from 'undefined' to '1'
26+
// Note y will not be changed because the scope of 'x' was passed in
27+
.attr( { x: "1", y: "2" } );
28+
29+
// restricted scope of an array of properties
30+
$("#el").attrChange([ "x", "y" ], report)
2531
// Change attr 'x' from 'undefined' to '1'
2632
// Change attr 'y' from 'undefined' to '2'
33+
// Note z will not be changed because the scope of 'x' and 'y' was passed in
2734
.attr( { x: "1", y: "2", z: "3" } );
28-
29-
// consolidating different mutation operations
30-
// rather than a 'dataChange' event for data() operations
31-
32-
$("#el").attrChange("data:foo", report)
35+
36+
// consolidating different mutation operations
37+
// rather than a 'dataChange' event for data() operations
38+
$("#el").attrChange("data:foo", report)
3339
// Change attr 'data:foo' from 'undefined' to 'bar'
3440
.data( "foo", "bar" )
3541
// Change attr 'data:!' from '[Object object]' to '[Object object]'
3642
.data( { } );
3743

38-
// works with val()
39-
$("#el").attrChange("val", report)
40-
// Change attr 'val' from 'hi' to 'bye'
44+
// works with val()
45+
$("#el").attrChange("val", report)
46+
// Change attr 'val' from 'hi' to 'bye'
4147
.val( 'bye' );
4248

4349
'attrChanging' event -- be notified before an attribute changes
@@ -46,16 +52,16 @@ Adds linkTo, linkFrom, and linkBoth plugins.
4652
.attrChanging(function(ev) {
4753
if (!confirm("Allow changing attr '" + ev.attrName + "' from '" +
4854
ev.oldValue + "' to '" + ev.newValue + "'?")) {
49-
55+
5056
ev.preventDefault();
5157
}
52-
58+
5359
})
5460
// Allow changing attr 'foo' from 'undefined' to 'bar'?
5561
// yes: value set, attrChange event raised
5662
// no: value not set, attrChange event not raised
5763
.attr("foo", "bar");
58-
64+
5965
'arrayChange' event -- be notified when an array is modified
6066

6167
var arr = [1,2,3];
@@ -65,7 +71,7 @@ Adds linkTo, linkFrom, and linkBoth plugins.
6571
});
6672
// Array operation push executed with args 4,5
6773
$.push(arr, 4, 5);
68-
74+
6975
// restricted scope
7076
$([arr])
7177
.arrayChange("push", function(ev) {
@@ -75,7 +81,7 @@ Adds linkTo, linkFrom, and linkBoth plugins.
7581
$.pop(arr);
7682
// Array operation push executed with args 4,5
7783
$.push(arr, 4, 5);
78-
84+
7985
$([arr])
8086
.arrayChange(["push", "pop", "splice"], function(ev) {
8187
alert("Array operation " + ev.change + " executed with args " + ev.arguments);
@@ -86,10 +92,10 @@ Adds linkTo, linkFrom, and linkBoth plugins.
8692
$.push(arr, 4, 5);
8793
// nothing
8894
$.splice(arr, 0, 1);
89-
95+
9096
'arrayChanging' event -- be notified before an array is modified
9197
// works just like 'attrChange', can cancel operation
92-
98+
9399
'linkTo' plugin -- bind the value of the current object to the value of another
94100

95101
var person = {};
@@ -99,17 +105,18 @@ Adds linkTo, linkFrom, and linkBoth plugins.
99105
alert(person.name); // foo
100106
// ... user changes value ...
101107
alert(person.name); // <user typed value>
102-
108+
103109
Support for converters -- modify the value as it flows across the link
104110

105111
var person = {};
112+
//TODO explain $.convertFn better including scope
106113
$.convertFn.round = function(value) {
107114
return Math.round( Math.parseFloat( value ) );
108115
}
109116
$("#name").linkTo("val", person, "age", "round");
110117
$("#name").val("7.5");
111118
alert(person.age); // 8
112-
119+
113120
'linkFrom' plugin -- bind the value of the current object from the value of another
114121

115122
var person = {};

0 commit comments

Comments
 (0)