Events firing twice, no clue why

2,664 views
Skip to first unread message

Kevin King

unread,
May 6, 2011, 2:29:41 PM5/6/11
to KnockoutJS
Hey guys,

I've noticed that some of my buttons/events are firing twice for no
apparent reason. Is there a stack tracing trick to figure out what's
actually invoking it?

I've seen this on products and drop down lists change events. It seems
like maybe it's getting registered twice maybe?

Any thoughts?

Thanks,

Kevin

rpn

unread,
May 6, 2011, 2:52:41 PM5/6/11
to knock...@googlegroups.com
Do you happen to be calling ko.applyBindings multiple times?

Kevin King

unread,
May 6, 2011, 3:41:12 PM5/6/11
to KnockoutJS
Ah yes, I am.

I had an issue where i was making an ajax call, updating the value of
a property on the viewModel, but it wasn't updating the UI.

It was a simple integer observable value.

Is there another way to update it? Or should i completely be
unneccessary?

Thanks,

Kevin

rpn

unread,
May 6, 2011, 4:05:11 PM5/6/11
to knock...@googlegroups.com
You should generally only need to call applyBindings one time, unless you are calling it specifically on new elements that have not been processed.  

Not sure why your UI wasn't updating.  Maybe you could share some of that code, if it is still happening.

Steven Sanderson

unread,
May 6, 2011, 4:51:46 PM5/6/11
to knock...@googlegroups.com
My guess is that your integer property isn't actually an observable,
or perhaps when you are updating it, you're replacing the entire
observable with a different one.

If you're able to show us the code that does the update, we can
probably identify the issue!

Steve

Kevin King

unread,
May 6, 2011, 5:13:34 PM5/6/11
to KnockoutJS
Absolutely, I don't mind at all =)

In the original viewModel creation here is how I assign the productid
property:

var viewModel = {};
viewModel.productid = ko.observable(-1);

Then, in a click event (on the view model), I make an ajax call to get
updated data. Some properties change including the productid in
certain cases.

In that code, i'm simply setting the value (In this case viewModelData
is a JS object that is parsed from the returned JSON)

viewModel.productid = viewModelData.productid;

I think you're right in that I'm replacing the entire property with a
new one. In this case, from an observable integer to a plain integer.

I have a feeling I'm going be saying "duh Kevin!" pretty soon.....

Thanks guys!

On May 6, 3:51 pm, Steven Sanderson <ste...@stevensanderson.com>
wrote:
> My guess is that your integer property isn't actually an observable,
> or perhaps when you are updating it, you're replacing the entire
> observable with a different one.
>
> If you're able to show us the code that does the update, we can
> probably identify the issue!
>
> Steve
>

Kevin King

unread,
May 9, 2011, 9:58:26 AM5/9/11
to KnockoutJS
Any thoughts on this?

Thanks!

Mark Bradley

unread,
May 9, 2011, 10:12:57 AM5/9/11
to knock...@googlegroups.com
On Mon, May 9, 2011 at 11:58 PM, Kevin King <kevi...@gmail.com> wrote:
> Any thoughts on this?
>
> Thanks!
>
> On May 6, 4:13 pm, Kevin King <kevin5...@gmail.com> wrote:
>> Absolutely, I don't mind at all =)
>>
>> In the original viewModel creation here is how I assign the productid
>> property:
>>
>> var viewModel = {};
>> viewModel.productid = ko.observable(-1);
>>
>> Then, in a click event (on the view model), I make an ajax call to get
>> updated data. Some properties change including the productid in
>> certain cases.
>>
>> In that code, i'm simply setting the value (In this case viewModelData
>> is a JS object that is parsed from the returned JSON)
>>
>> viewModel.productid = viewModelData.productid;

if you want to update the observable such that the ui updates you
should do this:

viewMode.productid(viewModelData.productid);

>>
>> I think you're right in that I'm replacing the entire property with a
>> new one. In this case, from an observable integer to a plain integer.
>>
>> I have a feeling I'm going be saying "duh Kevin!" pretty soon.....
>>
>> Thanks guys!
>>
>> On May 6, 3:51 pm, Steven Sanderson <ste...@stevensanderson.com>
>> wrote:
>>
>>
>>
>>
>>
>>
>>
>> > My guess is that your integer property isn't actually an observable,
>> > or perhaps when you are updating it, you're replacing the entire
>> > observable with a different one.
>>
>> > If you're able to show us the code that does the update, we can
>> > probably identify the issue!
>>
>> > Steve
>>
>> > On 6 May 2011, at 20:41, Kevin King <kevin5...@gmail.com> wrote:
>>
>> > > Ah yes, I am.
>>
>> > > I had an issue where i was making an ajax call, updating the value of
>> > > a property on the viewModel, but it wasn't updating the UI.
>>
>> > > It was a simple integer observable value.
>>
>> > > Is there another way to update it? Or should i completely be
>> > > unneccessary?
>>
>> > > Thanks,
>>
>> > > Kevin
>>
>> > > On May 6, 1:52 pm, rpn <rnieme...@gmail.com> wrote:
>> > >> Do you happen to be calling ko.applyBindings multiple times?

--
-barkmadley
sent from an internet enabled device

Mark Bradley

unread,
May 9, 2011, 10:15:35 AM5/9/11
to knock...@googlegroups.com
On Tue, May 10, 2011 at 12:12 AM, Mark Bradley <barkm...@gmail.com> wrote:
> On Mon, May 9, 2011 at 11:58 PM, Kevin King <kevi...@gmail.com> wrote:
>> Any thoughts on this?
>>
>> Thanks!
>>
>> On May 6, 4:13 pm, Kevin King <kevin5...@gmail.com> wrote:
>>> Absolutely, I don't mind at all =)
>>>
>>> In the original viewModel creation here is how I assign the productid
>>> property:
>>>
>>> var viewModel = {};
>>> viewModel.productid = ko.observable(-1);
>>>
>>> Then, in a click event (on the view model), I make an ajax call to get
>>> updated data. Some properties change including the productid in
>>> certain cases.
>>>
>>> In that code, i'm simply setting the value (In this case viewModelData
>>> is a JS object that is parsed from the returned JSON)
>>>
>>> viewModel.productid = viewModelData.productid;
>
> if you want to update the observable such that the ui updates you
> should do this:
>
> viewMode.productid(viewModelData.productid);

more information here:
http://knockoutjs.com/documentation/observables.html#reading_and_writing_observables

Kevin King

unread,
May 9, 2011, 11:13:58 AM5/9/11
to KnockoutJS
Perfect, doh! Thanks!

On May 9, 9:15 am, Mark Bradley <barkmad...@gmail.com> wrote:
> On Tue, May 10, 2011 at 12:12 AM, Mark Bradley <barkmad...@gmail.com> wrote:
> > On Mon, May 9, 2011 at 11:58 PM, Kevin King <kevin5...@gmail.com> wrote:
> >> Any thoughts on this?
>
> >> Thanks!
>
> >> On May 6, 4:13 pm, Kevin King <kevin5...@gmail.com> wrote:
> >>> Absolutely, I don't mind at all =)
>
> >>> In the original viewModel creation here is how I assign the productid
> >>> property:
>
> >>> var viewModel = {};
> >>> viewModel.productid = ko.observable(-1);
>
> >>> Then, in a click event (on the view model), I make an ajax call to get
> >>> updated data. Some properties change including the productid in
> >>> certain cases.
>
> >>> In that code, i'm simply setting the value (In this case viewModelData
> >>> is a JS object that is parsed from the returned JSON)
>
> >>> viewModel.productid = viewModelData.productid;
>
> > if you want to update the observable such that the ui updates you
> > should do this:
>
> > viewMode.productid(viewModelData.productid);
>
> more information here:http://knockoutjs.com/documentation/observables.html#reading_and_writ...
Reply all
Reply to author
Forward
0 new messages