Retrieve edited item

1 view
Skip to first unread message

micke

unread,
Feb 26, 2009, 10:03:13 AM2/26/09
to chainjs, mi...@yamba.se
Hi, first of all thanks for a great tool.
I wonder if I missed something, consider the following scenario:

$("#sample").item({ "name" : "steve" }).chain();

function save(){
alert($("#sample").item().name);
}

<div id="sample">
<input class="name" /><input type="button" onclick="save();"
value="update" />
</div>

What will be displayed in the alert() does not reflect any changes I
made in the input field, is there any way to retrieve the edited
object?

raid_ox

unread,
Feb 26, 2009, 3:58:42 PM2/26/09
to chainjs
hi, you still have to bind the "change" event to the chain:

by default, items won't be binded two-way to the DOM object. You need
to manually create an event listener, to send any change in the input
to the chain item.

$("#sample").item({ "name" : "steve" }).chain(function(){
var self = this;
self.find(':input').keyup(function(){
self.item({name: self.find('.name').val()});
})
});

maybe I could make this automatic in the next version. Do you have any
idea how the syntax should be?

micke

unread,
Feb 26, 2009, 5:33:31 PM2/26/09
to chainjs
Hi,

Since i am primary a C# developer, and unfortunately not a js guru i
cant give you a complete solution that properly extend chain etc., but
looking at the "Default Builder - Automatic Data filler" section of
Chain.js i think something along the lines of:

for (var i in data) {
self.find('> .' + i + ', *:not(.chain-element) .' + i)
.each(function() {
var match = $(this);
if (match.filter(':input').length) {
data[i] = match.val(); //just reversing the
assignment.
}
});
}

might do the trick :)

i believe that only getting the data when needed (if needed) would
also be more efficient than adding event listeners for potentially a
large number of properties.
but as i said i am no js guru ;)

/Micke

raid_ox

unread,
Feb 27, 2009, 1:14:06 AM2/27/09
to chainjs
What I mean with syntax was, syntax to conveniently add the Two-Way
binding such as:
chain({twoWay: ['name', ':input.name', 'onchange']})

Vincent Pérès

unread,
Feb 27, 2009, 4:16:24 AM2/27/09
to chainjs
Hello,

In that way :
<div id="sample">
<input class="name" />
<input type="button" value="update" class="update" />
</div>

I'm using :
$("#sample").item({ "name" : "steve" }).chain();
$('#sample').items(values) .chain({ '.update': function(data, el){
el.click(function(){
alert('You clicked on update button.');
});
});

Is that what you need?
Reply all
Reply to author
Forward
0 new messages