1

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!

rory
  • 1,490
  • 3
  • 22
  • 50

1 Answers1

4

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

Try this:

data-bind="css: {selectedTile: $root.selectedTile}"
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • pants, now the binding applies to every tile when I try to process it through the onclick event. I guess I really need to change up my datasource to keep it unique to the selected tile – rory May 22 '14 at 08:33
  • 1
    `selectedTile` sounds like it should hold an id or an object rather than a boolean :) – GôTô May 22 '14 at 08:40
  • I'm so close but just can't get there, any chance of a gentle push? I assume you mean in the viewmodel `selectedTile` should hold an id or object? – rory May 22 '14 at 09:23
  • 1
    Yes and then compare it to your current object in your binding like `data-bind="css: {selectedTile: $root.selectedTile() == myTileId()}"` – GôTô May 22 '14 at 09:28
  • 1
    goddammit I could've sworn I tried that earlier, I suppose I must have had it different somehow! Thanks so much for all your help! – rory May 22 '14 at 09:32