1.

Can You Have Multiple Knockout Models On One Page, And If So, How Would You Use Them?

Answer»

Yes, Knockout can bind to multiple models at one time. The KEY to keeping the different models isolated is to put any mark-up in separate div CONTAINERS, named with a unique ID, and to call the key ‘applyBindings’ method for each model, passing in the viewModel as the FIRST parameter, and the MATCHING div ID as the second parameter.

‘ko.applyBindings(MyFirstKOModel, document.getElementByID(‘firstDiv’))’.

‘ko.applyBindings(MySecondKOModel document.getElementByID(‘secondDiv’))’.

Yes, Knockout can bind to multiple models at one time. The key to keeping the different models isolated is to put any mark-up in separate div containers, named with a unique ID, and to call the key ‘applyBindings’ method for each model, passing in the viewModel as the first parameter, and the matching div ID as the second parameter.

‘ko.applyBindings(MyFirstKOModel, document.getElementByID(‘firstDiv’))’.

‘ko.applyBindings(MySecondKOModel document.getElementByID(‘secondDiv’))’.



Discussion

No Comment Found