淘汰与绑定导致“xxx不是一个功能”

时间:2014-01-18 12:46:22

标签: html5 knockout.js kendo-ui

您好我有以下代码:

<ul data-bind="with : weather()" >
    <li>Aktuelles Wetter (15:48)
    <ul>
       <li>
    <span style="float:right;" data-bind="text : weather()" onclick="alert(weatherViewModel.weather().wind.speed);"> km/h </span> 

我总是收到错误:Typerror:天气不是一个功能。 问题是,当我离开data-bind="with : weather"时,我没有遇到这个问题。 我虽然需要with-binding,因为我必须访问onclick属性中显示的属性。 有人可以帮忙吗?

更新:

var weatherViewModel = {

weather :  ko.observable(null),

forecast: ko.observable(null)
}

ViewModel的关键部分。使用kendo-ui通过Ajax请求设置Observable,如下所示:

change: function(e) {
    console.debug("weatherDataSource.change");
    console.log("action: " + e.action);
    if (e.action == "sync") {
        console.log("Retrieve weather data...");
        this.read();
    } else {
        var data = this.data();
        console.log(data[0]);
        weatherViewModel.weather(data[0]);
    }
}

但这很有效,因为我可以使用onclick-event访问数据。我需要with-binding的原因是因为ajax-request是在dom元素之后完成的,所以我无法访问weather()。wind.speed,因为weather()在开头是null。

1 个答案:

答案 0 :(得分:0)

&#34; with&#34;绑定创建一个上下文,该上下文中的所有绑定都自动引用父上下文。

所以你的&#34;文字&#34;数据绑定失败,因为天气里面没有天气功能。