Breeze.js query does not update knockout view

末鹿安然 提交于 2019-12-02 08:51:41

If you just using plain JS objects it will not work. Because it does not know when underlying data changes. Use mapping plugin for this to work:

$(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");
    });
});

Working code from Tomas without mappings. It is much faster:

$(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);;
});
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!