Visible binding with function having a parameter
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.
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')">
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?
精彩评论