Multiple item linking question.

1 view
Skip to first unread message

Mathew Byrne

unread,
Sep 22, 2008, 12:30:51 AM9/22/08
to chainjs
Looking for some clarity on how item linking works. It appears that
the rules for how links work are not 100% consistent. Are links meant
to be a one way thing?

Ok so take the following (contrived) example:

<ul id="list_1">
<li class="element">
<span class="name"></span>
</li>
</ul>
<ul id="list_2">
<li class="element">
<span class="name"></span>
</li>
</ul>
<script type="text/javascript" charset="utf-8">
var items = [{name: 'item 1'}, {name: 'item 2'}];
$('#list_1').items(items).chain();
$('#list_2').items('link', '#list_1', 'all').chain();

// Adds an item to both lists.
$('#list_1').items('add', {name: 'item 3'});

// Update the first item for both lists.
$('#list_1 li:eq(0)').item({name: 'an item'});

// Only adds the item to the second list.
$('#list_2').items('add', {name: 'item 4'});

// Updates the item for both lists.
$('#list_2 li:eq(0)').item({name: 'another item'});

// Only removes the item from the second list.
$('#list_2 li:eq(1)').item('remove');
</script>

The inconsistency appears to be that using the vanilla .item({...}) on
a linked item will work for both sets of lists, however using
item('remove') or item('add') will only affect the single set of
items, not both.

Just wondering if this is by design and whether linking is meant to be
read only?

Rizqi Ahmad

unread,
Sep 22, 2008, 1:05:26 AM9/22/08
to chainjs
It is by design. Linked items collection is "read only", but the items
in the linked collection is directly linked to the items in the
reference collection. because e.g. if the linked collection is
"selected", should it add new item when the linked item is adding new
item?

(By the way, I should prevent the linked item from adding a new item,
it is a bug actually)

do you think I should make it two ways?

Rizqi

Mathew Byrne

unread,
Sep 22, 2008, 1:12:47 AM9/22/08
to chainjs
I figured so! I like the idea of the linked item being read only,
since that's what I expected initially; it was only through my own
testing/playing around that I noticed the difference.

Making a two-way relationship seems like it should be something
separate (e.g. item('mirror/copy')) and potentially will more bug
prone and require more testing. For now if you lock down 'link' to be
mostly read only it might save some confusion.
Reply all
Reply to author
Forward
0 new messages