将多个参数发送到WinJS.Binding.converter()函数

时间:2013-02-05 18:01:27

标签: windows-8 winjs

有没有办法向WinJS.Binding.converter()函数发送多个参数?请考虑以下数据和输出:

        { contactName: "Tara Miller",  mainNumber: "555-405-6190", alternateNumber: "555-209-1927" },
        { contactName: "Bryan Bond",                               alternateNumber: "555-574-4270" },
        { contactName: "Jenna Siever", mainNumber: "555-843-8823", alternateNumber: "555-799-5424" },

enter image description here

这是HTML。 MyData.chooseBestNumber转换器功能用于显示某人的主要电话号码或“无主号码”字样,如果他们没有主号码:

<div id="listViewTemplate" data-win-control="WinJS.Binding.Template">
    <div class="contactCard">
        <div data-win-bind="innerText: contactName"></div>
        <div data-win-bind="innerText: mainNumber MyData.chooseBestNumber"></div>
    </div>
</div>

这是定义转换器功能的JS:

WinJS.Namespace.define("MyData", {
    chooseBestNumber: WinJS.Binding.converter(function (mainNumber) {
        if (mainNumber) return mainNumber;
        else return "no main number";
    }),
});

以下是我最终能够做到的...将多个参数传递给转换器函数,以便我可以返回主号码(如果已定义),备用号码(作为后备)或消息(如果所有其他方法都失败):

WinJS.Namespace.define("MyData", {
    chooseBestNumber: WinJS.Binding.converter(function (mainNumber, alternateNumber) {
        if (mainNumber) return mainNumber;
        else if (alternateNumber) return alternateNumber; 
        else return "no phone numbers";
    }),
});

是否可以向WinJS.Binding.converter()函数发送多个参数?

1 个答案:

答案 0 :(得分:7)

您实际上可以将您的电话号码div绑定到this关键字,这将有效地将该div的innerText属性绑定到整个模型对象。在您的转换器中,您可以访问整个模型。

因此,您更新的代码如下所示:

<强> HTML

<div id="listViewTemplate" data-win-control="WinJS.Binding.Template">
    <div class="contactCard">
        <div data-win-bind="innerText: contactName"></div>
        <div data-win-bind="innerText: this MyData.chooseBestNumber"></div>
    </div>
</div>

JavaScript转换器

WinJS.Namespace.define("MyData", {
    chooseBestNumber: WinJS.Binding.converter(function (model) {
        if (model && model.mainNumber) return mainNumber;
        else if (model && model.alternateNumber) return alternateNumber;
        else return "no main number";
    }),
});