Skip to content

Commit 6e379ef

Browse files
joefeserDave Reed
authored andcommitted
Added p tags where needed
1 parent 1702fe8 commit 6e379ef

File tree

1 file changed

+81
-36
lines changed

1 file changed

+81
-36
lines changed

README.md

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<h1>Introduction</h1>
2-
2+
<p>
33
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').
4-
4+
</p>
55
<h2>Mutation Events</h2>
66
In order to link a source to a target, it is necessary to be notified when a data associated with the source object changes, so that it can be pushed onto the target object. This plugin adds some special events to jQuery to facilitiate this, which are also useful on their own.
77

88
<h3>attrChange</h3>
9-
9+
<p>
1010
The 'attrChange' event fires when an attribute of a DOM element or object is changed through the jQuery.fn.attr or jQuery.attr methods. An interesting feature of this plugin is that it specifically allows for jQuery.fn.attr to be usable on plain objects or arrays. The data(), bind(), and trigger() methods all work with plain objects, so this is a natural extension which already mostly works. However, a small change was necessary to jQuery.attr to avoid the special cases applied when the target is a plain object, like class->className, and readonly->readOnly, and that negative values of "width" are ignored, etc. So this plugin also makes it officially possible to use attr() to set fields of an object as you would expect.
11-
11+
</p>
1212
<pre>
1313
function report(ev) {
1414
alert("Change attr '" + ev.attrName + "' from '" +
@@ -36,9 +36,9 @@ $("#el").attrChange([ "x", "y" ], report)
3636
// Note no event for 'z' because the scope of 'x' and 'y' was passed in
3737
.attr( { x: "1", y: "2", z: "3" } );
3838
</pre>
39-
39+
<p>
4040
The attrChange event can also be used to capture changes made through the val() and data() methods. Notice that special treatment is given to how the change is represented by the event. This consolidation of the different mutation methods causing the same event makes it simpler to handle and prevents the need for separate "dataChange" and "valChange" events. It would be nice, actually, if attr() was thought of as a general purpose mutation method and also supported this construct. For example, $(..).attr("data:foo", "bar") or $(..).attr("val", "123"). However, that is not implemented and open to discussion.
41-
41+
</p>
4242
<pre>
4343
// works with data()
4444
$("#el").attrChange("data:foo", report)
@@ -55,7 +55,9 @@ $("#el").attrChange("val", report)
5555

5656
<h3>attrChanging</h3>
5757

58+
<p>
5859
The 'attrChanging' event fires when an attribute of a DOM element or object is about to be changed. The ev.preventDefault() method may be called in order to prevent the change from occuring.
60+
</p>
5961

6062
<pre>
6163
$("#el")
@@ -74,7 +76,9 @@ $("#el")
7476

7577
<h3>arrayChange</h3>
7678

79+
<p>
7780
Like the attrChange event, but fires when an Array is mutated through any of the new array mutation APIs. Information about what the mutation was is available on the event object.
81+
</p>
7882

7983
<pre>
8084
var arr = [1,2,3];
@@ -86,10 +90,12 @@ $([arr])
8690
$.push(arr, 4, 5);
8791
</pre>
8892

93+
<p>
8994
The following array mutation events are available as static methods on the jQuery object: push, pop, splice, shift, unshift, reverse, sort. The arguments supported for each are exactly like their built-ins, except the array is passed as the first parameter.
90-
95+
</p>
96+
<p>
9197
Like 'attrChange', the 'arrayChange' event supports filtering by the operation.
92-
98+
</p>
9399
<pre>
94100
$([arr])
95101
.arrayChange(["push", "pop", "splice"], function(ev) {
@@ -105,16 +111,21 @@ $.splice(arr, 0, 1);
105111

106112
<h3>arrayChanging</h3>
107113

114+
<p>
108115
Exactly like the attrChanging event, but for arrays. Operation can be cancelled via the ev.preventDefault() method.
116+
</p>
109117

118+
<h2>Linking Objects</h2>
110119

111-
<h2>Linking Objects<h2>
112-
120+
<p>
113121
When objects are linked, changes to one are automatically forwarded to another. For example, this allows you to very quickly and easily link fields of a form to an object. Any changes to the form fields are automatically pushed onto the object, saving you from writing retrieval code. Furthermore, built-in support for converters lets you modify the format or type of the value as it flows between objects (for example, formatting a phone number, or parsing a string to a number).
122+
</p>
114123

115124
<h3>jQuery.link</h3>
116125

126+
<p>
117127
Sets up a link that pushes changes to any of the source objects to all target objects.
128+
</p>
118129

119130
<pre>
120131
var person = {};
@@ -128,13 +139,15 @@ alert(person.name); // foo
128139
alert(person.name); // <user typed value>
129140
</pre>
130141

131-
The 'source' may be an object, DOM element, or string selector.
132-
<strong>object</strong>
133-
Changes to that object through a jQuery set wrapping it will trigger the link. e.g.:
142+
<p>
143+
The 'source' may be an object, DOM element, or string selector.<br/>
144+
<strong>object</strong><br/>
145+
Changes to that object through a jQuery set wrapping it will trigger the link. e.g.:<br/>
134146
<code>$(obj).attr("foo", "bar");</code>
135-
136-
<strong>DOM element or selector</strong>
147+
<br/>
148+
<strong>DOM element or selector</strong><br/>
137149
This sets up a link from all the matching elements (if it is a selector) to all matching targets. For example, if there are 3 inputs and 3 spans on the page, 9 links are created, one from each input to each span.
150+
</p>
138151

139152
<pre>
140153
$.link({
@@ -143,36 +156,42 @@ $.link({
143156
});
144157
</pre>
145158

146-
<strong>Attributes and Microdata</strong>
159+
<strong>Attributes and Microdata</strong><br/>
147160
The 'sourceAttr' and 'targetAttr' fields are optional. If omitted, the attribute is determined automatically:
148-
161+
<p>
149162
The source attribute is determined as follows:
150163
input, textarea, or select: "val"
151164
any other dom element: "text"
152-
165+
</p>
166+
<p>
153167
The target attribute is determined as follows:
154168
Source is a DOM element, and has 'itemprop' microdata attribute? Use the value.
155169
Otherwise, use source.name or source.id.
156170
If source is not a DOM element, use the same rules as source attribute.
157-
171+
</p>
172+
<p>
158173
This allows for simple links that target complex scenarios. For example, the following creates a link from all input elements inside #form1 to a single target object. The field set on the object is determined by first seeing if the input causing the event has an 'itemprop' attribute. If not, the input's name or id is used. In this example, the target's 'fullName' and 'birthday' fields would be set.
159-
174+
</p>
160175
<pre>
161176
$.link({ source: "#form1 input", target: contact });
162177

163178
<input type="text" itemprop="fullName" name="name" />
164179
<input type="text" name="birthday" />
165180
</pre>
166181

182+
<p>
167183
For example, the following sets up a link that activates whenever the val() of the input changes, and reacts by setting the text() of the span.
184+
</p>
168185

169186
<pre>
170187
$.link( { source: "#input1", target: "#span1" } );
171188
</pre>
172189

173-
<strong>From/To Syntax</strong>
190+
<strong>From/To Syntax</strong><br/>
174191

192+
<p>
175193
$.link supports creating multiple links with different rules at the same time. In this example, two form elements are mapped to two different fields of the same object.
194+
</p>
176195

177196
<pre>
178197
$.link({
@@ -186,7 +205,9 @@ $.link({
186205
});
187206
</pre>
188207

208+
<p>
189209
Each value specified can be an array, or not. If not, the one value is applied to all cases. If an array, corresponding indexes of each array are used to create each link. Note that each source may also still match multiple elements if it is a selector. The full syntax is:
210+
</p>
190211

191212
<pre>
192213
$.link({
@@ -206,13 +227,17 @@ $.link({
206227
});
207228
</pre>
208229

209-
<strong>twoWay</strong>
230+
<strong>twoWay</strong><br/>
210231

232+
<p>
211233
The twoWay option sets up links in both directions -- from source to target, and target to source. Changes in either will be reflected in the other. This is the reason for the 'convert' option on the 'to' settings -- those converters would be used when pushing changes from a target to a source (reverse).
234+
</p>
212235

213-
<strong>Updating immediately</strong>
236+
<strong>Updating immediately</strong><br/>
214237

238+
<p>
215239
Sometimes it is desired that the target of a link reflect the source value immediately, even before the source is changed. You can tell link() to update the target immediately using the 'update' setting:
240+
</p>
216241

217242
<pre>
218243
$.link({ source: source, target: target, update: true });
@@ -227,14 +252,16 @@ $.link({
227252
});
228253
</pre>
229254

255+
<p>
230256
Note that this is particularly useful when relying on the automatic target attribute determination. You can quickly populate an object with a form's current values by relying on <code>itemprop</code> attributes or input <code>name</code>, and setting update to true to force an immediate update.
231-
257+
</p>
258+
<p>
232259
Note that if you put 'update' on the 'from' settings, the source is updated with the target value, even though the link usually flows from the source to the target. This allows you, for example, to setup a link from an input to an object, but have the input initially reflect the value already in the target.
233-
234-
<strong>Context</strong>
235-
260+
</p>
261+
<strong>Context</strong><br/>
262+
<p>
236263
$.link in both direct and from/to forms allows a 2nd jQuery context parameter. This context is used if any selectors are given. For example, these are equivalent:
237-
264+
</p>
238265
<pre>
239266
$.link({
240267
from: {
@@ -258,16 +285,20 @@ $.link({
258285

259286
<h3>jQuery.unlink</h3>
260287

288+
<p>
261289
This removes a link previously established with $.link. The syntax is exactly like $.link, including the from/to syntax, except that the 'convert' and 'update' parameters are not used. A link is identified by four pieces of data: source, sourceAttr, target, and targetAttr. Note that it is possible to remove only a portion of a link previously created. For example, assuming there are two elements on the page with css class "foo" (#foo1 and #foo2), the following creates two links -- one from each, then removes only one of them.
290+
</p>
262291

263292
<pre>
264293
$.link( { source: ".foo", target: target, targetAttr: "field" } );
265294
$.unlink( { source: "#foo1", target: target, targetAttr: "field" } );
266295
</pre>
267296

268-
<strong>Automatic unlinking</strong>
297+
<strong>Automatic unlinking</strong><br/>
269298

299+
<p>
270300
Links are cleaned up when its target or source is a DOM element that is being destroyed. For example, the following setups a link between an input and a span, then destroys the span by clearing it's parent html. The link is automatically removed.
301+
</p>
271302

272303
<pre>
273304
$.link( { source: "#input1", target: "#span1" } );
@@ -276,7 +307,9 @@ $("#span1").parent().html("");
276307

277308
<h3>jQuery.linkLive</h3>
278309

310+
<p>
279311
$.liveLink is a powerful tool that links multiple elements now or in the future. For example, to map all the input fields of a form to an object, even when form fields are dynamically added in the future:
312+
</p>
280313

281314
<pre>
282315
$.liveLink({
@@ -289,18 +322,24 @@ $.liveLink({
289322
});
290323
</pre>
291324

325+
<p>
292326
Note however that currently you cannot use 'twoWay' on a live link. You may use 'update'.
327+
</p>
293328

294329
<h3>jQuery.unlinkLive</h3>
295330

331+
<p>
296332
Removes a live link previously created with $.linkLive. Syntax is the same as unlink. Note that unlike regular links, live links do not expand into all the possible sources and targets when they are created. This means you cannot 'unliveLink' a portion of a live link, you may only remove the entire live link.
333+
</p>
297334

298335
<h3>Conversion and jQuery.convertFn</h3>
299336

337+
<p>
300338
Often times, it is necessary to modify the value as it flows from one side of a link to the other. For example, to convert null to "None", to format or parse a date, or parse a string to a number. The link APIs support specifying a converter function, either as a name of a function defined on jQuery.convertFn, or as a function itself.
301-
339+
</p>
340+
<p>
302341
The plugin comes with one converter named "!" which negates the value.
303-
342+
</p>
304343
<pre>
305344
var person = {};
306345
$.convertFn.round = function(value) {
@@ -311,7 +350,9 @@ $("#name").val("7.5");
311350
alert(person.age); // 8
312351
</pre>
313352

353+
<p>
314354
It is nice to reuse converters by naming them this way. But you may also specified the converter directly as a function.
355+
</p>
315356

316357
<pre>
317358
var person = {};
@@ -322,7 +363,9 @@ $("#name").val("7.5");
322363
alert(person.age); // 8
323364
</pre>
324365

366+
<p>
325367
Converter functions receive the value that came from the source as the first parameter. They also receive a settings object which corresponds to the parameters given to the link API (if the from/to syntax was used, the settings are expanded into the more granular source/target form). This allows you to easily parameterize a converter.
368+
</p>
326369

327370
<pre>
328371
var person = {};
@@ -336,7 +379,9 @@ $("#name").val("red");
336379
alert(person.age); // #FF0000
337380
</pre>
338381

382+
<p>
339383
The settings object also contains the source and target parameters. Say you wanted to link two different fields on the source to one field the target, as in combining the first and last name fields of an object onto a single "full name" span.
384+
</p>
340385

341386
<pre>
342387
var person = { firstName: "Some", lastName: "User" };
@@ -355,8 +400,8 @@ alert($("#fullname").text()); // "jQuery User"
355400

356401
<h1>Revision History</h1>
357402

358-
* 5/26/2010 -- Completely revised the API based on forum feedback.
359-
* 5/01/2010 -- Corrected comments about restricted scope -- event is suppressed, not the change.
360-
* 5/01/2010 -- Fixed glitches in comments and added info about restricted scope.
361-
* 4/29/2010 -- Expanded on converter samples.
362-
* 4/28/2010 -- Initial proposal published
403+
* 5/26/2010 -- Completely revised the API based on forum feedback.<br />
404+
* 5/01/2010 -- Corrected comments about restricted scope -- event is suppressed, not the change.<br />
405+
* 5/01/2010 -- Fixed glitches in comments and added info about restricted scope.<br />
406+
* 4/29/2010 -- Expanded on converter samples.<br />
407+
* 4/28/2010 -- Initial proposal published<br />

0 commit comments

Comments
 (0)