javascript - 数据绑定(bind)到 className winjs

标签 javascript data-binding windows-8.1 windows-phone-8.1 winjs

我有一个包含按钮的中继器。按钮的类应根据其状态进行设置。

如何将 State: 属性数据绑定(bind)到 CSS className

数据

items = new WinJS.Binding.List([
    {
        JobOperations: [
            { AccountType: "Internal", Index: 1, Requirements: "Do something to something else", Rework: true, State: "onHold", LoadHours: "5.7" },
            { AccountType: "Retail", Index: 2, Requirements: "Watch something for a duration of time", Rework: true, State: "complete", LoadHours: "2.1" }
        ]
    }
]);

HTML 和绑定(bind)

<div id="rptOperations" data-win-control="WinJS.UI.Repeater" class="buttonList operationsList" data-win-options="{data: items}">
    <button data-win-bind="className: State" class="button">
        <span data-win-bind="textContent: Index" class="index">1/span>
        <span data-win-bind="textContent: Requirements" class="requirements"></span>
        <span data-win-bind="textContent: AccountType" class="accountType"></span>
        <span data-win-bind="textContent: LoadHours" class="loadHours"></span>
    </button>
</div>

期望的输出

(请注意按钮上的 onHoldcomplete 类)

<div id="rptOperations" class="buttonList operationsList">
    <button class="button onHold">
        <span class="index">1</span>
        <span class="requirements">Do something to something else</span>
        <span class="accountType">Internal</span>
        <span class="loadHours">5.7</span>
    </button>
    <button class="button complete">
        <span class="index">2</span>
        <span class="requirements">Watch something for a duration of time</span>
        <span class="accountType">Retail</span>
        <span class="loadHours">2.1</span>
    </button>
</div>

上面的代码是本示例的简化版本。一切都在实际应用程序中完美运行,直到我尝试绑定(bind)到 className 这导致应用程序崩溃。

如果我不能这样做,最好的方法是什么?在发布此内容之前,我在谷歌上进行了广泛的搜索,但找不到太多关于该主题的信息。

最佳答案

如果没有更多代码,很难说清楚,但我看到人们在 WinJS 中遇到问题,因为他们不在代码中调用 Binding.processAll

app.onactivated = function (args) {

    // Other activation code ...

    var personDiv = document.querySelector('#nameSpan');
    WinJS.Binding.processAll(personDiv, person);
    var bindingSource = WinJS.Binding.as(person);
}

一般来说,您可以引用这两个教程解决大多数问题,但如果您认为您已经完全正确,我们需要更多代码来帮助!引用:http://msdn.microsoft.com/en-us/library/windows/apps/hh700358.aspx , http://msdn.microsoft.com/en-us/library/windows/apps/Hh700356.aspx

根据您的情况,您可以定义命名空间和数据,如下所示:

 "use strict";
    WinJS.Namespace.define("MyAppData", {
        items: new WinJS.Binding.List([
           { AccountType: "Internal", Index: 1, Requirements: "Do something to something else", Rework: true, State: "onHold", LoadHours: "5.7" },
            { AccountType: "Retail", Index: 2, Requirements: "Watch something for a duration of time", Rework: true, State: "complete", LoadHours: "2.1" }
        ])
    });

然后将其连接到您的控件,如下所示:

 <div id="rptOperations" data-win-control="WinJS.UI.Repeater" class="buttonList operationsList" data-win-options="{data: MyAppData.items}">
        <button data-win-bind="className: State" class="button">
            <span data-win-bind="textContent: Index" class="index"></span>
            <span data-win-bind="textContent: Requirements" class="requirements"></span>
            <span data-win-bind="textContent: AccountType" class="accountType"></span>
            <span data-win-bind="textContent: LoadHours" class="loadHours"></span>
        </button>
    </div>

关于javascript - 数据绑定(bind)到 className winjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26428263/

相关文章:

windows-8 - Moto X 未显示在 Eclipse Windows 8.1 中

javascript - 背景图片不显示

c# - 自定义 datagridview 列上的数据绑定(bind)问题

c# - 是否可以绑定(bind)一个asp :GridView to a List<T>?

c# - WPF MessageBox 取消复选框检查

javascript - Google Maps API V3 双指缩放不适用于 Windows 8.1 预览版上的 IE 11

javascript - 为什么 DOM 事件处理程序中的嵌套函数会修复 'this' 绑定(bind)

javascript - Firefox - 当某些变量与某些模式匹配时拦截/修改发布数据

javascript - Angular-google-maps 未在引导选项卡中加载

windows - 是否有用于在 Windows 8.1 上启用/禁用蓝牙的 API?