Binding to an object created via constructor function

29 views
Skip to first unread message

Josh Huckabee

unread,
Nov 2, 2010, 7:17:44 PM11/2/10
to KnockoutJS
Can anyone provide any insight into what I'm doing wrong here?

Essentially I have a view model that contains an observable
attribute. The value of that attribute is an object that gets created
via a constructor function. Whenever I update the value of that
attribute with a new object, the bindings seem to break. (See the
demo in this pastie: http://pastie.org/1268130. I've commented the
test case that is breaking.)

If I change the value of the text field, the original object's value
gets updated, not the current object's value. If I manually set the
value by issuing viewModel.item().stamp('some stamp'), the value is
correctly updated and the text field is updated as well. So it seems
like its working half-way.

Any thoughts?

Ω Alisson

unread,
Nov 2, 2010, 10:19:01 PM11/2/10
to knock...@googlegroups.com
That's exactly my problem too!

Steven Sanderson

unread,
Nov 3, 2010, 4:33:27 AM11/3/10
to knock...@googlegroups.com
Hi Josh and Alisson

Thanks for reporting this. Particularly, thanks for the clear repro test case.

I've investigated and can confirm this is an issue with the current version of Knockout (internally, the "change" handler it registers on the textbox only gets registered on initialisation, and so it retains a reference to the original observable). I could give you some awkward steps to work around this, but to be honest it will be better just to fix it in KO itself. I've raised the issue at https://github.com/SteveSanderson/knockout/issues/issue/13 and will aim to fix it this week.

Regards
Steve


2010/11/3 Ω Alisson <thelin...@gmail.com>

fla...@gmail.com

unread,
Nov 5, 2010, 5:20:31 AM11/5/10
to KnockoutJS
Hello again

I've now implemented a fix for this problem. Can you grab the latest
build from https://github.com/SteveSanderson/knockout/tree/master/build/output/
and check that the problem is gone for you?

To fix this problem I was forced to change the API for registering
bindings. If you've written any custom bindings of your own, you'll
need to update your method signatures slightly. Basically, if you
previously wrote this:

ko.bindingHandlers.myCustomBinding = {
init: function(element, value, allBindings, viewModel) {
// Now do something with value/allBindings
},
update: function(element, value, allBindings, viewModel) {
// Now do something with value/allBindings
}
};

... then from version 1.1.1, you need to change it to this:
ko.bindingHandlers.myCustomBinding = {
init: function(element, valueAccessor, allBindingsAccessor,
viewModel) {
var value = valueAccessor(), allBindings =
allBindingsAccessor();
// Now do something with value/allBindings
},
update: function(element, valueAccessor, allBindingsAccessor,
viewModel) {
var value = valueAccessor(), allBindings =
allBindingsAccessor();
// Now do something with value/allBindings
}
};

If you haven't written any custom bindings, you don't need to change
anything, and everything should just work.

I'll write a further post here later to emphasise this custom binding
API change and explain why it was necessary, and will also post a
proper documentation page to knockoutjs.com about creating custom
bindings. However I can't publish the new documentation until KO
version 1.1.1 is actually released otherwise it will confuse people.
So, it would be great if you could give me your feedback about this
fix ASAP so I can finalise the 1.1.1 release.

Thanks very much!
Steve


On Nov 3, 8:33 am, Steven Sanderson <ste...@stevensanderson.com>
wrote:
> Hi Josh and Alisson
>
> Thanks for reporting this. Particularly, thanks for the clear repro test
> case.
>
> I've investigated and can confirm this is an issue with the current version
> of Knockout (internally, the "change" handler it registers on the textbox
> only gets registered on initialisation, and so it retains a reference to the
> original observable). I could give you some awkward steps to work around
> this, but to be honest it will be better just to fix it in KO itself. I've
> raised the issue athttps://github.com/SteveSanderson/knockout/issues/issue/13and will aim to
> fix it this week.
>
> Regards
> Steve
>
> 2010/11/3 Ω Alisson <thelinuxl...@gmail.com>
>
>
>
>
>
>
>
> > That's exactly my problem too!
>

Ω Alisson

unread,
Nov 5, 2010, 12:52:27 PM11/5/10
to knock...@googlegroups.com
Hi Steven, now it's working!
I wish there was a way to update the new object with the options already selected on the form.
Reply all
Reply to author
Forward
0 new messages