Breeze.js查询不会更新knockout视图

时间:2013-06-17 14:29:42

标签: data-binding mvvm knockout.js breeze

我通过breeze.js查询数据,这是第一次正常工作。第二次没有更新视图。

HTML

<a id="linkQDate">Order by Newest</a>
<a id="linkQScore">Order by Score</a>

<div id="questionWrapper">
    <ul data-bind="foreach: results">
    <li>
       <strong><span data-bind="text: Score"></span></strong>
       <span data-bind="text: Titel"></span>
    </li>
    </ul>
</DIV>

JS

$(document).ready(function () {
var manager = new breeze.EntityManager('/breeze/dbentities');
var isApplied = false;
var dmodel;

$("#linkQDate").click(function () {
    var query = breeze.EntityQuery.from("Questions").orderBy("Date");
    manager.executeQuery(query).then(querySucceeded);
    function querySucceeded(data) {
        dmodel = data;
        if (!isApplied) {

            ko.applyBindings(dmodel, $("#questionWrapper")[0]);
            isApplied = true;
       }
    }

});
$("#linkQScore").click(function () {
    var query = breeze.EntityQuery.from("Questions").orderBy("Score");
    manager.executeQuery(query).then(querySucceeded);
    function querySucceeded(data) {
        dmodel = data;
        if (!isApplied) {
            ko.applyBindings(dmodel, $("#questionWrapper")[0]);
            isApplied = true;
        }
    }

});
});

2 个答案:

答案 0 :(得分:1)

如果你只是使用普通的JS对象,它将无法正常工作。因为它不知道底层数据何时发生变化。使用mapping plugin可以实现此目的:

$(document).ready(function () {
    var manager = new breeze.EntityManager('/breeze/dbentities');
    var isApplied = false;
    var dmodel;

    function querySucceeded(data) {
        if (!isApplied) {
            dmodel = ko.mapping.fromJS(data);
            ko.applyBindings(dmodel, $("#questionWrapper")[0]);
            isApplied = true;
        } else {
            ko.mapping.fromJS(data, dmodel);
        }
    }

    $("#linkQDate").click(function () {
        var query = breeze.EntityQuery.from("Questions").orderBy("Date");
        manager.executeQuery(query).then(querySucceeded);
    });

    $("#linkQScore").click(function () {
        var query = breeze.EntityQuery.from("Questions").orderBy("Score");
    });
});

答案 1 :(得分:0)

Tomas的工作代码没有映射。它要快得多:

$(document).ready(function () {
var manager = new breeze.EntityManager('/breeze/dbentities');
var isApplied = false;

var dmodel = { results: ko.observableArray() };

function queryFailed(data) {
                console.log(data);
            }
function querySucceeded(data) {
    if (!isApplied) {
        for (var i = 0; i < data.results.length; i++) {
            dmodel.results.push(data.results[i]);

        }

        ko.applyBindings(dmodel, $("#questionWrapper")[0]);
        isApplied = true;
    } else {
        dmodel.results.removeAll();

        for (var i = 0; i < data.results.length; i++) {
            dmodel.results.push(data.results[i]);
        }
    }
}

$("#linkQDate").click(function () {
    var query = breeze.EntityQuery.from("Questions").orderBy("Date");
    manager.executeQuery(query).then(querySucceeded).fail(queryFailed);;
});

$("#linkQScore").click(function () {
    var query = breeze.EntityQuery.from("Questions").orderBy("Score");
    manager.executeQuery(query).then(querySucceeded).fail(queryFailed);;
});
});