Tom Rider
Tom Rider

Reputation: 2815

Knockout observable property is not updating

I am using knockout.js plugin in my application. I have a problem with observable. I created an Example Fiddle. In this fiddle i am creating Menus dynamically and generating Menu sequence each time when new menu is added. User can to delete any existing menu also except first one.

The problem comes when user delete any existing menu. Because at the time of deletion i am updating the sequence number in the remove function but the sequence numbers are not updating. Can anybody please tell me whats the problem ?

Upvotes: 1

Views: 458

Answers (2)

JNo
JNo

Reputation: 457

Solution: Fiddle

Modified:

newMenuVM.Sequence = ko.observable(self.menus().length+1);

self.menus()[i].Sequence(i+1);

Deleted:

self.courseOptions.push(newMenuVM);

Reason:

When I run your fiddle, I see an error "Property 'Sequence' of object # is not a function".

I search this property and see:

newMenuVM.Sequence = self.menus().length+1;

So, I add ko.observable function.

Upvotes: 1

Aran Mulholland
Aran Mulholland

Reputation: 23945

You are redefining the Sequence variable. Originally it is a ko.observable. You are setting it to a integer value. You should set the value as so

newMenuVM.Sequence(self.menus().length+1);

This will allow the observable to notify and update the value on screen.

Upvotes: 1

Related Questions