Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Commit f2b13ff

Browse files
committed
Support for jQuery 1.5. Previous versions of Data Link will NOT work correctly with jQuery 1.5.
Provides a .setField() method to replace the .data() method, which has been modified in jQuery 1.5.
1 parent 7862f45 commit f2b13ff

File tree

3 files changed

+99
-41
lines changed

3 files changed

+99
-41
lines changed

README.md

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ Documentation for the _jQuery Data Link_ plugin can be found on the jQuery docum
55

66
<p>
77
==================================== WARNING ====================================<br/>
8-
Note: This plugin currently depends on jQuery version 1.4.3.<br/>
8+
<b>Note:</b> <br/>
9+
10+
In jQuery 1.5 the behavior of the <i>$( somePlainObject ).data()</i> method has been changed.
11+
12+
This build of the <i>jQuery DataLink</i> plugin will work with both jQuery 1.5, and previous builds of jQuery.
13+
14+
It provides a new <i>$( somePlainObject ).setField()</i> method, which should be used instead of <i>.data()</i>,
15+
for modifying field values on plain objects and triggering data linking.
916
=================================================================================
1017
</p>
1118

1219

1320
<h1>Introduction</h1>
1421
<p>
15-
This proposal describes how support for "data linking" can be added to the jQuery core library. The term "data linking" is used here to mean "automatically linking the field of an object to another field of another object." That is to say, the two objects are "linked" to each other, where changing the value of one object (the 'source') automatically updates the value in the other object (the 'target').
22+
This is the official jQuery DataLink plugin. The term "data linking" is used here to mean "automatically linking the field of an object to another field of another object." That is to say, the two objects are "linked" to each other, where changing the value of one object (the 'source') automatically updates the value in the other object (the 'target').
1623
</p>
1724

1825
<h2>jQuery(..).link() API</h2>
@@ -22,14 +29,29 @@ The link API allows you to very quickly and easily link fields of a form to an o
2229
</p>
2330

2431
<pre>
25-
var person = {};
26-
$("form").link(person);
27-
$("#name").val("foo");
28-
alert(person.name); // foo
29-
// ... user changes value ...
30-
alert(person.name); // &lt;user typed value&gt;
31-
$(person).data("name", "bar");
32-
alert($("#name").val()); // bar
32+
&lt;script>
33+
$().ready(function() {
34+
var person = {};
35+
$("form").link(person);
36+
37+
$("[name=name]").val("NewValue"); // Set firstName to a value.
38+
alert(person.name); // NewValue
39+
40+
$(person).setField("name", "NewValue");
41+
alert($("[name=name]").val()); // NewValue
42+
43+
// ... user changes value ...
44+
$("form").change(function() {
45+
// &lt;user typed value&gt;
46+
alert(person.name);
47+
});
48+
});
49+
&lt;/script>
50+
51+
&lt;form name="person">
52+
&lt;label for="name">Name:&lt;/label>
53+
&lt;input type="text" name="name" id="name" />
54+
&lt;/form>
3355
</pre>
3456

3557
<p>
@@ -60,22 +82,41 @@ Often times, it is necessary to modify the value as it flows from one side of a
6082
<p>
6183
The plugin comes with one converter named "!" which negates the value.
6284
</p>
85+
6386
<pre>
64-
var person = {};
65-
$.convertFn.round = function(value) {
66-
return Math.round( Math.parseFloat( value ) );
67-
}
68-
$("#age").link(person, {
69-
age: {
70-
convert: "round"
87+
&lt;script>
88+
$().ready(function() {
89+
var person = {};
90+
91+
$.convertFn.round = function(value) {
92+
return Math.round( parseFloat( value ) );
7193
}
94+
95+
$("#age").link(person, {
96+
age: {
97+
convert: "round"
98+
}
99+
});
100+
101+
/* Once the user enters their age, the change event will fire which, in turn, will
102+
* cause the round function to be called. This will then round the age up or down,
103+
* set the rounded value on the object which will then cause the input field to be
104+
* updated with the new value.
105+
*/
106+
$("#age").change(function() {
107+
alert(person.age);
108+
});
72109
});
73-
$("#name").val("7.5");
74-
alert(person.age); // 8
110+
&lt;/script>
111+
112+
&lt;form name="person">
113+
&lt;label for="age">Age:&lt;/label>
114+
&lt;input type="text" name="age" id="age" />
115+
&lt;/form>
75116
</pre>
76117

77118
<p>
78-
It is nice to reuse converters by naming them this way. But you may also specified the converter directly as a function.
119+
It is convenient to reuse converters by naming them this way. But you may also specify the converter directly as a function.
79120
</p>
80121

81122
<pre>
@@ -87,6 +128,7 @@ $("#age").link(person, {
87128
}
88129
}
89130
});
131+
90132
$("#name").val("7.5");
91133
alert(person.age); // 8
92134
</pre>
@@ -126,7 +168,7 @@ $("#rank").link(product, {
126168
}
127169
}
128170
});
129-
$(product).data("salesRank", 12);
171+
$(product).setField("salesRank", 12);
130172
alert($("#rank").height()); // 24
131173
</pre>
132174
<p>

demos/demo-contacts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function refresh() {
6161
}
6262
}
6363
});
64-
$( contact ).trigger( "changeData", ["age", contact.age] );
64+
$( contact ).trigger( "changeField", ["age", contact.age] );
6565

6666
// todo: "update" feature
6767

@@ -73,8 +73,8 @@ function refresh() {
7373
original_lastName = contact.lastName;
7474
$( ".contact-reset", this ).click( function() {
7575
$( contact )
76-
.data( "firstName", original_firstName )
77-
.data( "lastName", original_lastName );
76+
.setField( "firstName", original_firstName )
77+
.setField( "lastName", original_lastName );
7878
});
7979

8080
$( "tr.phone", this ).each( function(i) {

jquery.datalink.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,17 @@
66
* Dual licensed under the MIT or GPL Version 2 licenses.
77
* http://jquery.org/license
88
*/
9-
(function($){
9+
(function( $, undefined ){
1010

1111
var oldcleandata = $.cleanData,
1212
links = [],
1313
fnSetters = {
1414
val: "val",
1515
html: "html",
1616
text: "text"
17-
};
18-
19-
function setValue(target, field, value) {
20-
if ( target.nodeType ) {
21-
var setter = fnSetters[ field ] || "attr";
22-
$(target)[setter](value);
23-
} else {
24-
$(target).data( field, value );
25-
}
26-
}
17+
},
18+
eventNameSetField = "setField",
19+
eventNameChangeField = "changeField";
2720

2821
function getLinks(obj) {
2922
var data = $.data( obj ),
@@ -33,10 +26,10 @@ function getLinks(obj) {
3326
}
3427

3528
function bind(obj, wrapped, handler) {
36-
wrapped.bind( obj.nodeType ? "change" : "changeData", handler );
29+
wrapped.bind( obj.nodeType ? "change" : eventNameChangeField, handler );
3730
}
3831
function unbind(obj, wrapped, handler) {
39-
wrapped.unbind( obj.nodeType ? "change" : "changeData", handler );
32+
wrapped.unbind( obj.nodeType ? "change" : eventNameChangeField, handler );
4033
}
4134

4235
$.extend({
@@ -72,12 +65,30 @@ $.extend({
7265
"!": function(value) {
7366
return !value;
7467
}
68+
},
69+
setField: function(target, field, value) {
70+
if ( target.nodeType ) {
71+
var setter = fnSetters[ field ] || "attr";
72+
$(target)[setter](value);
73+
} else {
74+
var parts = field.split(".");
75+
parts[1] = parts[1] ? "." + parts[1] : "";
76+
77+
var $this = jQuery( target ),
78+
args = [ parts[0], value ];
79+
80+
$this.triggerHandler( eventNameSetField + parts[1] + "!", args );
81+
if ( value !== undefined ) {
82+
target[ field ] = value;
83+
}
84+
$this.triggerHandler( eventNameChangeField + parts[1] + "!", args );
85+
}
7586
}
7687
});
7788

78-
function getMapping(ev, changed, newvalue, map, source, target) {
89+
function getMapping(ev, changed, newvalue, map) {
7990
var target = ev.target,
80-
isSetData = ev.type === "changeData",
91+
isSetData = ev.type === eventNameChangeField,
8192
mappedName,
8293
convert,
8394
name;
@@ -138,7 +149,7 @@ $.extend($.fn, {
138149
value = convert( value, ev.target, target );
139150
}
140151
if ( value !== undefined ) {
141-
setValue( target, name, value );
152+
$.setField( target, name, value );
142153
}
143154
}
144155
},
@@ -158,7 +169,7 @@ $.extend($.fn, {
158169
newvalue = convert( newvalue, target, this );
159170
}
160171
if ( newvalue !== undefined ) {
161-
setValue( this, "val", newvalue );
172+
$.setField( this, "val", newvalue );
162173
}
163174
});
164175
}
@@ -235,6 +246,11 @@ $.extend($.fn, {
235246
}
236247
}
237248
});
249+
},
250+
setField: function(field, value) {
251+
return this.each(function() {
252+
jQuery.setField( this, field, value );
253+
});
238254
}
239255
});
240256

0 commit comments

Comments
 (0)