0

我正在通过第一次运行良好的微风.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;
        }
    }

});
});
4

2 回答 2

1

如果您只使用普通的 JS 对象,它将无法正常工作。因为它不知道底层数据何时发生变化。使用映射插件为此工作:

$(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");
    });
});
于 2013-06-17T16:25:45.550 回答
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);;
});
});
于 2013-06-17T20:12:09.710 回答