Search code examples
data-bindingknockout.jsphonejsdevextreme

knockout.js/devExtreme unable to process css-binding


I have a dxTileView template which is databound to data coming in from the server. Then, in the template I want to add a css binding which adds a class when the tile is clicked like so...

<div data-bind="dxTileView: {itemClickAction:assignProject, dataSource:quickBooking}">
          <div data-options="dxTemplate : { name:'item' }" data-bind="css: {selectedTile: selectedTile}">
            <h4 data-bind="text: blah"></h4>
            <p data-bind="text: blah"></p>
          </div>
</div>

And then in the viewmodel I have:

var viewModel = {
        selectedTile: ko.observable(false),
        blah: blah}

And the error message:

Uncaught ReferenceError: Unable to process binding "css: function (){return {selectedTile:selectedTile} }"
Message: selectedTile is not defined 

I have tried a few variations on my bindings as follows but none work:

data-bind="css: {selectedTile: viewModel.selectedTile}"//error: viewModel not defined
data-bind="css: {selectedTile: viewModel.selectedTile()}"
data-bind="css: {selectedTile: true}" //this works,was just for testing
data-bind="css: {selectedTile: selectedTile(false)}"//etc etc etc

Many thanks in advance for any help!


Solution

  • To access the root of your view model, use $root.

    Try this:

    data-bind="css: {selectedTile: $root.selectedTile}"