4

I am getting the following error:

Microsoft JScript runtime error: Unable to parse binding attribute.
Message: TypeError: Object expected; Attribute value: visible:
IsVisible('Area')

I am trying to hide / show my html elements based on some evaluation. This is my code:

 var viewModel = {
            propertyTypeList: ko.observableArray([]),
            selectedPropertyType: ko.observable(""),
            visibleFeatures: ko.observableArray([]),
            IsVisible : function(featureName){some logic here}
        };

And this is the view:

<div class="editor-field">
    <select data-bind="options: propertyTypeList, 
                       optionsText: 'PropertyTypeName', 
                       value: selectedPropertyType, 
                       optionsCaption: 'select property type...'">
    </select>
</div>

<div class="editor-label" data-bind="visible: IsVisible('Area')">
    Area
</div>
<div class="editor-label" data-bind="visible: IsVisible('Bedroom')">
  Bedroom
</div>

The function IsVisible will do some evaluation based on the selectedPropertyType and the feature name and will return true or false.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Pinakin Shah
  • 877
  • 2
  • 12
  • 25
  • 1
    It should work; A similar example works for me > http://jsfiddle.net/EhEsd/ . Does the select data-binding works? – neebz Jul 12 '11 at 12:36
  • text and value binding working for me and visible binding works if there is a boolean property in the view model. Not working when it is a function – Pinakin Shah Jul 12 '11 at 12:43
  • can you show what you are doing inside the IsVisible function? It should be returning a true/false. Your above code looks all set, there is something else which is causing the bug. – neebz Jul 12 '11 at 13:13
  • You are right. I think the return type of IsVisible was incorrect – Pinakin Shah Jul 14 '11 at 12:27
  • http://stackoverflow.com/questions/6706281/knockoutjs-can-we-create-a-dependentobservable-function-with-a-parameter – Pinakin Shah Jul 16 '11 at 13:27
  • 2
    Pinakin Shah, I assume you have solved the problem already. Could you please post you solution and mark it as an accepted answer to help the community? – Roman Bataev Feb 07 '12 at 18:38
  • sure. Missed this one. will update shortly – Pinakin Shah Feb 10 '12 at 05:30

2 Answers2

1

If you want the visibility of those divs to change dynamically (which it seems that you might), you will want to to make isVisible a computedObservable. That will make sure that the isVisible logic gets re-run when observables (that it depends on) change.

it could return a string which you could bind you visibility to (data-bind="visible: isVisible() === 'Area'"), or you could create a different computed for each div if they are unrelated. (isAreaVisilbe, isBedroomVisible, ...) Your viewModel essentially turns into something like this:

function ViewModel() {
    var self = this;
    self.propertyTypeList = ko.observableArray(['Area', 'Bedroom', 'Other']);
    self.selectedPropertyType = ko.observable("");
    self.visibleFeatures = ko.observableArray([]);
    self.visibleDiv = ko.computed(function () {
        //Your logic here
        return self.selectedPropertyType();
    });

I made a basic working example of what I think you are trying to make here http://jsfiddle.net/ZqWDS/5/ Had to change your viewModel to be a function so that the isVisible property can reference other properties. (How to reference properties of current object in JS). And you can read the section titled 'A popular convention that simplifies things' on the knockout computedObservables page for more details about why I structured the viewModel constructor that way.

I'm not sure what's causing your error at the top of the question. I pasted your code into a jsFiddle and it essentially worked (with a few minor syntax edits) http://jsfiddle.net/GBkdK/

If you are still having issues and want to provide some more context, I'm happy to help.

EDIT Sorry, I think I was wrong about isVisible needing to be a computed. It appears that it could just be a function like you originally had it. http://jsfiddle.net/ZqWDS/6/

knockoutjs: can we create a dependentObservable function with a parameter?

Community
  • 1
  • 1
Mat
  • 96
  • 1
  • 4
1

Try calling the method in following way

<div class="editor-label" data-bind="visible: $root.IsVisible('Area')">

or

<div class="editor-label" data-bind="visible: viewModel.IsVisible('Area')">
Saurabh
  • 207
  • 2
  • 9