|
| 1 | +A jQuery live bindings plugin - created for demonstration purposes. |
| 2 | +Adds 'attrChanging', 'attrChange', 'arrayChanging', 'arrayChange' events. |
| 3 | +Adds jQuery.x array operations: pop, push, splice, sort, reverse, shift, unshift |
| 4 | +Adds linkTo, linkFrom, and linkBoth plugins. |
| 5 | + |
| 6 | +'attrChange' event -- be notified when an attribute is changed. |
| 7 | + |
| 8 | + function report(ev) { |
| 9 | + alert("Change attr '" + ev.attrName + "' from '" + |
| 10 | + ev.oldValue + "' to '" + ev.newValue + "'."); |
| 11 | + } |
| 12 | + |
| 13 | + $("#el").attrChange(report) |
| 14 | + // Change attr 'foo' from 'undefined' to 'bar' |
| 15 | + .attr("foo", "bar"). |
| 16 | + // Change attr 'foo' from 'bar' to 'baz' |
| 17 | + .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) |
| 25 | + // Change attr 'x' from 'undefined' to '1' |
| 26 | + // Change attr 'y' from 'undefined' to '2' |
| 27 | + .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) |
| 33 | + // Change attr 'data:foo' from 'undefined' to 'bar' |
| 34 | + .data( "foo", "bar" ) |
| 35 | + // Change attr 'data:!' from '[Object object]' to '[Object object]' |
| 36 | + .data( { } ); |
| 37 | + |
| 38 | + // works with val() |
| 39 | + $("#el").attrChange("val", report) |
| 40 | + // Change attr 'val' from 'hi' to 'bye' |
| 41 | + .val( 'bye' ); |
| 42 | + |
| 43 | +'attrChanging' event -- be notified before an attribute changes |
| 44 | + |
| 45 | + $("#el") |
| 46 | + .attrChanging(function(ev) { |
| 47 | + if (!confirm("Allow changing attr '" + ev.attrName + "' from '" + |
| 48 | + ev.oldValue + "' to '" + ev.newValue + "'?")) { |
| 49 | + |
| 50 | + ev.preventDefault(); |
| 51 | + } |
| 52 | + |
| 53 | + }) |
| 54 | + // Allow changing attr 'y' from 'undefined' to '2'? |
| 55 | + // yes: value set, attrChange event raised |
| 56 | + // no: value not set, attrChange event not raised |
| 57 | + .attr("foo", "bar"); |
| 58 | + |
| 59 | +'arrayChange' event -- be notified when an array is modified |
| 60 | + |
| 61 | + var arr = [1,2,3]; |
| 62 | + $([arr]) |
| 63 | + .arrayChange(function(ev) { |
| 64 | + alert("Array operation " + ev.change + " executed with args " + ev.arguments); |
| 65 | + }); |
| 66 | + // Array operation push executed with args 4,5 |
| 67 | + $.push(arr, 4, 5); |
| 68 | + |
| 69 | + // restricted scope |
| 70 | + $("push", [arr]) |
| 71 | + .arrayChange(function(ev) { |
| 72 | + alert("Array operation " + ev.change + " executed with args " + ev.arguments); |
| 73 | + }); |
| 74 | + // nothing |
| 75 | + $.pop(arr); |
| 76 | + // Array operation push executed with args 4,5 |
| 77 | + $.push(arr, 4, 5); |
| 78 | + |
| 79 | + $(["push", "pop", "splice"], [arr]) |
| 80 | + .arrayChange(function(ev) { |
| 81 | + alert("Array operation " + ev.change + " executed with args " + ev.arguments); |
| 82 | + }); |
| 83 | + // Array operation pop executed with args undefined |
| 84 | + $.pop(arr); |
| 85 | + // Array operation push executed with args 4,5 |
| 86 | + $.push(arr, 4, 5); |
| 87 | + // nothing |
| 88 | + $.splice(arr, 0, 1); |
| 89 | + |
| 90 | +'arrayChanging' event -- be notified before an array is modified |
| 91 | + // works just like 'attrChange', can cancel operation |
| 92 | + |
| 93 | +'linkTo' plugin -- bind the value of the current object to the value of another |
| 94 | + |
| 95 | + var person = {}; |
| 96 | + $("#name").linkTo("val", person, "name"); |
| 97 | + alert(person.name); // $("#name").val() |
| 98 | + $("#name").val("foo"); |
| 99 | + alert(person.name); // foo |
| 100 | + // ... user changes value ... |
| 101 | + alert(person.name); // <user typed value> |
| 102 | + |
| 103 | +'linkFrom' plugin -- bind the value of the current object from the value of another |
| 104 | + |
| 105 | + var person = {}; |
| 106 | + $(person).linkFrom("name", "#name", "val"); |
| 107 | + alert(person.name); // $("#name").val() |
| 108 | + $("#name").val("foo"); |
| 109 | + alert(person.name); // foo |
| 110 | + // ... user changes value ... |
| 111 | + alert(person.name); // <user typed value> |
| 112 | + |
| 113 | +'linkBoth' plugin -- links the values of two objects to each other |
| 114 | + var person = {}; |
| 115 | + $(person).linkBoth("name", "#name", "val"); |
| 116 | + alert(person.name); // $("#name").val() |
| 117 | + $("#name").val("foo"); |
| 118 | + alert(person.name); // foo |
| 119 | + $(person).attr("name", "bob"); |
| 120 | + alert($("#name").val()); // bob |
0 commit comments