Search code examples
javascriptarraysbuttonknockout.jsenable-if

KnockoutJS: based on length of array enable button


In a project we're doing, we have created an inbox where the users (amongst other things) can select the items. If a user selects an item, the button should be enabled -- if none are selected, the button should be disabled.

Simple enough, but for the life of me, I can't get it working :-(

You can find a fiddle here: http://jsfiddle.net/rzrfp/

I presume I'm missing something very, very, VERY simple and stupid, but I've been tried virtually everything, and can't get it working ...


Solution

  • Use ko.computed instead of ko.computable.
    Or even better: use data-bind="enable: selectedItems().length > 0" in your button element. So you can omit the computed showButton.

    EDIT: To use the right context use var modelImpl = new myModel("myParam");. Otherwise this inside the model refers to the window object.