可观察项目在敲除绑定中具有零元素

时间:2013-07-11 00:06:24

标签: breeze knockout-2.0

我遇到有线问题,在应用自定义绑定之前和之后,Array的Record计数是8,但是当调试绑定时,它们Array是空的。由于数组为空,因此Grid不显示任何内容。我做错了什么?

var vM = {
            recordsCount: ko.observable(),
            countries: ko.observableArray()
        };
        $(function () {

        getAllCountries();

        // Handler for .ready() called.
        //alert(myns.JavaScript1);
        //alert(myns.JavaScript1);
        //alert(myns.JavaScript2);

    });


    var serviceName = "/breeze/CountriesBreeze/";
    var manager = new breeze.EntityManager(serviceName);

    /***  supporting functions ***/
    function getAllCountries() {
        var query = breeze.EntityQuery.from("GetCountries");
        console.log("Getting Countries");
        return manager.executeQuery(query)
            .then(function(data) {
                vM.countries(data.results);
                alert(vM.countries().length);
                ko.applyBindings(vM);
                alert(vM.countries().length);
            }).fail(
                alert( "Error getting Data")
            )
    }


</script>
<div id="log"></div>

                               ]

                   }">
      </table>

1 个答案:

答案 0 :(得分:0)

ko.applyBindings()语句移至此处:

var vM = {
            recordsCount: ko.observable(),
            countries: ko.observableArray()
        };
        $(function () {

        getAllCountries();

        // Handler for .ready() called.
        //alert(myns.JavaScript1);
        //alert(myns.JavaScript1);
        //alert(myns.JavaScript2);

    });

ko.applyBindings(vM);

然后我不确定只是做vM.countries(data.results)是否有效,你可能需要迭代并推送每个值或者执行:

vM.countries(data.results)
vM.countries.valueHasMutated()