0

I have the following view-model

var viewModel = {
FLFSExamIsH: ko.observable(true),
FLFSROIEntryFieldDynPosBottom: ko.computed(function() {
    return this.FLFSExamIsH ? "flfs_ROIImagesRow3Pos" : "flfs_ROIImagesRow2Pos";
}),

};

in my html page I am binding a div as follows:

data-bind='css: FLFSROIEntryFieldDynPosBottom'

at some point in my code, I execute: viewModel.FLFSExamIsH(false);

But at all times (in the chrome debugger) viewModel.FLFSROIEntryFieldDynPosBottom() is always returning the 'flfs_ROIImagesRow2Pos', regardless of the update to the viewmodel: viewModel.FLFSExamIsH()

Setting a breakpoint on the computed function and updating that model variable doesn't re-evaluate the computed function either.

2 Answers2

3

You are missing parenthesis at

return this.FLFSExamIsH ? "flfs_ROIImagesRow3Pos" : "flfs_ROIImagesRow2Pos";
//                    ^^^ here, it should be this.FLFSExamIsH()

Plus since you are using not using a function to define your viewmodel, the computed should be added after (check this thread for more info):

var viewModel = {
    FLFSExamIsH: ko.observable(true),
};
viewModel.FLFSROIEntryFieldDynPosBottom = ko.computed(function() {
    return this.FLFSExamIsH() 
                 ? "flfs_ROIImagesRow3Pos" 
                 : "flfs_ROIImagesRow2Pos";
});

Demo

Community
  • 1
  • 1
GôTô
  • 7,974
  • 3
  • 32
  • 43
0

You can't use the this keyword if you do not use a function constructor.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
Anders
  • 17,306
  • 10
  • 76
  • 144