问题描述:

I am trying to enable a save button based on the change events of various UI elements.

The save button needs to be enabled if any of the following happens

1) Input data changes

2) Or if the cities of the user list size changes.

How do I track the list size change event ?

and

How do I enable the save button for both the events in knockout ?

网友答案:

You should use a computed

this.canSave = ko.computed(function() {
   return this.observableOne() === "foo" && this.observableTwo() === "bar";
}, this);

edit: Or use dirty logic like http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html

网友答案:

Try using subscribe events. Have a observable say prevlength and set it to the length here.

this.myObservable.subscribe(function(previousValue){
    //get previousval length and set it to another observable say prevlength
}, this, "beforeChange");

and again use subscribe without beforechange, and check prevlength with the current length. Use an observable say shouldEnable and compare the length with prevlength. If the length changes update shouldEnable to true or set to false.

And for disabling concept based on condition, you can check Enable condition for click binding

相关阅读:
Top