Knockout js "IF" binding to a function receiving parameters and returning boolean
knockout.js
Solution
Comments by @nemesv are spot on: you probably have an error or problem in code you're not showing in your question. Nonetheless, here's a setup that may help you find and fix that error you're experiencing.
Your method signature `IsInRole` isn't congruent with the input parameter, which is an array of roles. If you change that to `IsInAnyRole` things may become clearer, something like this should work just fine:
<div data-bind="if: isInAnyRole(['admin', 'editor'])">Protected div!</div>
With the following view model:
var vm = function() {
var self = this;
self.roles = ko.observableArray(["editor", "user"]);
self.isInAnyRole = function(targetRoles) {
return targetRoles.some(function(el) { return self.roles().indexOf(el) !== -1; });
}
};
You can check out this fiddle for a demo of the above. If you change the roles of the view model to something other than "editor" or "admin" the message will disappear.
Problem
I want to be able to show or hide certain DOM elements based on a array of say role names. The thing is that I want to check the role or roles in html. Something like: ``` <div data-bind="if: isInRole('Admin', 'Editor')"> ``` or ``` <div data-bind="if: isInRole(['Admin', 'Editor'])"> ``` The above solution doesn't seem to work for me. Any suggestions/alternatives?