共用方式為


WinJS.Binding.converter function

Creates a default binding initializer for binding between a source property and a destination property with the specified converter function that is executed on the value of the source property.

Syntax

var functionRef = WinJS.Binding.converter(convert);

Parameters

  • convert
    Type: Function

    The conversion function that takes the source property and produces a value that is set to the destination property. This function must be accessible from the global namespace.

Return value

Type: Function

The binding initializer.

Remarks

Caution  When you use the converter, it marks the specified function as supported for declarative processing. A function that is marked as supported for declarative processing can be called from static HTML, potentially allowing an attacker to perform privileged actions. For this reason, only mark a function as supported for declarative processing if you are completely confident that it is secure from the injection of third-party content.

 

Examples

The following code shows how to create a converter function that takes the currently logged-on user type property and maps it to the background color of the corresponding SPAN element. The function is defined inside the LoginData namespace, which is defined with WinJS.Namespace.define.

...
<span id="loginName" 
    data-win-bind="innerText: name; style.color: userType LoginData.userTypeToColor">
</span>
...

WinJS.Binding.processAll(document.getElementById("loginDisplay"), LoginData.login);

WinJS.Namespace.define("LoginData", {
        //Data Object
        login : { name: "myname", id: "12345678", userType: "admin" },

        //Converter function
        userTypeToColor: WinJS.Binding.converter(function (type) {
            return type == "admin" ? "Green" : "Red";
        })
    });

See the Declarative Binding sample for an example of a conversion function that does not use the converter method.

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Binding