Search code examples
javascriptractivejs

Is it possible to have computed function in RactiveJS?


Is there any way to run a computed property with an argument? Pretty much a computed function.

JS code

computed: {
            IsShapeToolSelected: function (toolId) {
               return toolId == this.get("QueryTool.QueryByShapeTool") ? "selected-tool" : "";
            }
          }

Html

<button on-click='draw: {{0}}' type="button" class="btn btn-default {{IsShapeToolSelected(0)}}">
   <span style="font-size:18px;">&#9679;</span>
   <br />
   <span>point</span>
</button>
<button on-click='draw: {{1}}' type="button" class="btn btn-default {{IsShapeToolSelected(1)}}">
   <img src="Content/imgs/idv.MapTools_Reports/line-24x24.png" />
   <br />
   <span>line</span>
</button>

Solution

  • Is there any way to run a computed property with an argument?

    No, but there are other solutions for your problem:

    computed: {
        IsShapeToolSelected: function () {
            return (function (toolId) {
                return toolId == this.get("QueryTool.QueryByShapeTool") ? "selected-tool" : "";
            }).bind(this);
        }
    }
    

    or

    data: {
        IsShapeToolSelected: function (toolId) {
            return toolId == this.get("QueryTool.QueryByShapeTool") ? "selected-tool" : "";
        }
    }
    

    or

    computed: {
        // use IsShapeToolSelected.1 instead of IsShapeToolSelected(1)
        IsShapeToolSelected: function () {
            var obj = {};
            obj[this.get("QueryTool.QueryByShapeTool")] = "selected-tool";
            return obj;
        }
    }