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

Commit 1b7cd73

Browse files
committed
Made datalink.html show more aspects of API.
Working towards support of hierarchical declarative binding.
1 parent a4a4636 commit 1b7cd73

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed

beta2/datalink.html

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<button onclick="setCity()">set city</button>
1919
<hr /><br />
2020

21-
<div id="myContainer">
21+
<div id="myDeclarative">
2222
<p data-jq-bind="css-background-color: roleColor">
2323
Name: <span data-jq-bind="firstName, css-background-color: roleColor"></span> <span data-jq-bind="lastName"></span>
2424
</p>
@@ -28,6 +28,20 @@
2828
<input data-jq-link="address.city" data-jq-bind="address.city" />
2929
<input data-jq-link="roleColor" data-jq-bind="roleColor" />
3030
</p>
31+
<p>
32+
<span data-jq-bind="nameConvert( firstName )"></span>
33+
<span data-jq-bind="address.city"></span>
34+
</p>
35+
36+
</div>
37+
38+
<div id="myExplicit">
39+
<p class="cityWrapper">
40+
City: <span class="city"></span>
41+
</p>
42+
<p>
43+
<input name="city" />
44+
</p>
3145
</div>
3246

3347
<br /><hr />
@@ -40,34 +54,61 @@
4054
firstName: "Jo",
4155
lastName: "Proctor",
4256
address: {
43-
city: "Redmond",
57+
city: "Redmond"
4458
},
4559
roleColor: "yellow"
4660
},
4761
person2 = {
4862
firstName: "Jo2",
4963
lastName: "Proctor2",
5064
address: {
51-
city: "Redmond2",
65+
city: "Redmond2"
5266
},
5367
roleColor: "green"
5468
},
5569
person3 = {
5670
firstName: "Jo3",
5771
lastName: "Proctor3",
5872
address: {
59-
city: "Redmond3",
73+
city: "Redmond3"
6074
},
6175
roleColor: "red"
6276
};
6377

64-
$.dataLink( person, "#myContainer" ).push();
78+
$.dataLink( person, "#myDeclarative" ).push();
6579

66-
$.dataLink( "#myContainer", person );
80+
$.dataLink( "#myDeclarative", person );
6781

6882
$.dataLink( person, person2 );
6983
$.dataLink( person, person3 ).push();
7084

85+
$.dataLink( person, "#myExplicit", [
86+
{
87+
from: "address.city",
88+
to: ".city"
89+
},
90+
{
91+
from: "address.city",
92+
to: "input[name = city]"
93+
},
94+
{
95+
from: "roleColor",
96+
to: ".cityWrapper",
97+
toAttr: "css-background-color"
98+
},
99+
{
100+
from: "address.city",
101+
to: "input[name = city]",
102+
toAttr: "title",
103+
convert: titleConvert
104+
}
105+
]).push();
106+
107+
$.dataLink( "#myExplicit", person, {
108+
from: "input[name = city]",
109+
to: "address.city"
110+
});
111+
71112
function nameConvert( value ) {
72113
return value + " lives in";
73114
}

beta2/jquery.datalink2.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ function addBinding( map, from, to, callback, links ) {
353353
ev.stopImmediatePropagation();
354354
}
355355
};
356+
356357
var j, l;
358+
357359
switch ( fromType + toType ) {
358360
case "htmlarray" :
359361
for ( j=0, l=toObj.length; j<l; j++ ) {
@@ -368,6 +370,20 @@ function addBinding( map, from, to, callback, links ) {
368370
}
369371
break;
370372

373+
case "objecthtml" :
374+
if ( thisMap.decl ) {
375+
to = to.find( "[ data-jq-bind ]" ).add( to.filter( "[ data-jq-bind ]" ) );
376+
// thisMap.to = 0;
377+
}
378+
// TODO Replace with this style for better perf:
379+
// elems = elem.getElementsByTagName("*");
380+
// for ( m = elems.length - 1; m >= 0; m-- ) {
381+
// processItemKey( elems[m] );
382+
// }
383+
// processItemKey( elem );
384+
from.bind( eventType, handler );
385+
break;
386+
371387
default:
372388
from.bind( eventType, handler );
373389
}
@@ -416,10 +432,6 @@ function objectType( object ) {
416432
: "none";
417433
}
418434

419-
function declarativeMap( fromType, toType ) {
420-
return !unsupported[ fromType + toType ] && $.extend( {}, decl.from[fromType], decl.to[toType], { decl: true } );
421-
}
422-
423435
function wrapObject( object ) {
424436
return object instanceof $ ? object : $.isArray( object ) ? $( [object] ) : $( object ); // Ensure that an array is wrapped as a single array object
425437
}
@@ -482,7 +494,7 @@ $.extend({
482494
args[3] = args.pop();
483495
return $.dataLink.apply( $, args );
484496
}
485-
var i,
497+
var i, fromType, toType,
486498
links = [],
487499
linkset = { // TODO Consider exposing as prototype, for extension
488500
links: links,
@@ -514,8 +526,17 @@ $.extend({
514526
if ( from ) {
515527
from = wrapObject( from );
516528
to = wrapObject( to );
517-
518-
if ( maps = maps || declarativeMap( objectType( from[ 0 ]), objectType( to[ 0 ]))) {
529+
fromType = objectType( from[ 0 ]);
530+
toType = objectType( to[ 0 ]);
531+
maps = maps
532+
|| !unsupported[ fromType + toType ]
533+
&& {
534+
decl: true,
535+
from: fromType === "html" ? "input[" + linkAttr + "]" : undefined//,
536+
// to: toType === "html" ? "[" + bindAttr + "]" : undefined
537+
};
538+
539+
if ( maps ) {
519540
maps = $.isArray( maps ) ? maps : [ maps ];
520541

521542
i = maps.length;
@@ -581,18 +602,6 @@ $.extend({
581602
// toAttr: toPath convert( toPath ) end
582603
bindInfo.replace( /(?:([\w\-]+)\:\s*)?(?:(?:([\w\.]+)|(\w+)\(\s*([\w\.]+)\s*\))(?:$|,))/g, setTarget );
583604
}
584-
},
585-
from: {
586-
object: {},
587-
html: {
588-
from: "input[" + linkAttr + "]"
589-
}
590-
},
591-
to: {
592-
object: {},
593-
html: {
594-
to: "[" + bindAttr + "]"
595-
}
596605
}
597606
},
598607
merge: {

0 commit comments

Comments
 (0)