Knockout.js breeze经理在成功回访完成前返回承诺

Knockout.js breeze经理在成功回访完成前返回承诺,knockout.js,Knockout.js,我遇到了一个严重的问题,那就是绑定一个来自breeze电话的淘汰赛 在这里发布我的代码 function objCart(object_id, brand_name, model_name, size, name, imagepath, brand_imagepath, model_imagepath, quantity, offerAmount, showCartProductImage)

我遇到了一个严重的问题,那就是绑定一个来自breeze电话的淘汰赛

在这里发布我的代码

function objCart(object_id,
             brand_name, model_name,
             size, name, imagepath,
             brand_imagepath, model_imagepath,
             quantity, offerAmount, showCartProductImage) {

    this.object_id = ko.observable(object_id);
    this.brand_name = ko.observable(brand_name);
    this.model_name = ko.observable(model_name);
    this.size = ko.observable(size);
    this.name = ko.observable(name);
    this.imagepath = ko.observable(imagepath);
    this.brand_imagepath = ko.observable(brand_imagepath);
    this.model_imagepath = ko.observable(model_imagepath);
    this.quantity = ko.observable(quantity);
    this.offerAmount = ko.observable(offerAmount);
    this.showCartProductImage = ko.observable(showCartProductImage);
    this.editEnable = ko.observable(false);
    this.showEdit = ko.observable(true);
    this.showCancel = ko.observable(false);
    this.cancelEdit = ko.observable(false);
    this.offerEdit = function () {
        if (!this.editEnable()) {
            this.editEnable(true);
            this.showCancel(true);
            this.showEdit(false);
            gCartEditQuantity = this.quantity();
            gCartEditOfferAmount = this.offerAmount();
        }
    }
    this.CancelEdit = function () {

        this.editEnable(false);
        this.showEdit(true);
        this.showCancel(false);
        this.quantity(gCartEditQuantity);
        this.offerAmount(gCartEditOfferAmount);

    }

    this.offerUpdate = function () {

        this.editEnable(false);
        this.showEdit(true);
        this.showCancel(false);         
        UpdateCart(this.object_id, this.quantity, this.offerAmount, vm.customerId);

    }

    this.offerDelete = function () {
        DeleteCart(this.object_id, this.quantity, this.offerAmount, vm.customerId);
        // cartItems.remove(this);
    }


    this.ShowItemDetailsModal = function () {

        getItemDetails(this.object_id);
        modaldialog.show(detailsView).then(function () {
            //logger.log('Modal Closed', null, 'home', true);
        });

    }


    this.ShowBrandDetailsModal = function () {

        getBrandDetails(this.object_id);
        modaldialog.show(brandDetails).then(function () {
            //logger.log('Modal Closed', null, 'home', true);
        });

    }

}
正在进行如下服务器调用以获取数据

function getCartItems(customerId) {

    var query = breeze.EntityQuery.
            from("getCart")
            .withParameters({ CustomerId: customerId
            })
            .orderBy("name");
    var promise = manager
        .executeQuery(query)
        .then(querySucceeded)
        .fail(queryFailed);

    function querySucceeded(data) {

        vm.cartItems([]);
        var temp = [];
        for (var i = 0; i < data.results.length; i++) {
            var crt = new objCart(data.results[i].object_id,
                               data.results[i].brand_name,
                               data.results[i].model_name,
                               data.results[i].size,
                               data.results[i].name,
                               data.results[i].imagepath,
                               data.results[i].brand_imagepath,
                               data.results[i].model_imagepath,
                               data.results[i].quantity,
                               data.results[i].offerAmount,
                               data.results[i].showCartProductImage);
            temp.push(crt);


        }

        vm.cartItems(temp);
        vm.cartItems.valueHasMutated();
        if (vm.cartItems().length > 0) {

            itemsInCart(true);
            checkoutDisplay(true);
        }
        else {

            itemsInCart(false);
            checkoutDisplay(false);
        }

    }

    function queryFailed(error) {
        toastr.error("Query failed: " + error.message);
    }

    return promise;
};
函数getCartItems(customerId){ var query=breeze.EntityQuery。 从(“getCart”) .withParameters({CustomerId:CustomerId }) .订购人(“名称”); var promise=经理 .executeQuery(查询) .然后(查询成功) 。失败(查询失败); 函数查询成功(数据){ vm.cartItems([]); var-temp=[]; 对于(var i=0;i0){ itemsInCart(正确); 签出显示(真); } 否则{ itemsInCart(假); 签出显示(假); } } 函数查询失败(错误){ toastr.error(“查询失败:+error.message”); } 回报承诺; }; 问题是——当第一次加载视图时,列表什么也不显示,即使cartItems()中有项

对于后续的routernavigates,它会在上一次调用的viewmodel中显示包含项目的列表

我认为问题是因为breeze经理在成功回调映射实体之前返回promize

但我找不到更好的办法来解决它


感谢您的反馈和建议。

我没有一个很好的答案,但也许有一个想法可以解决它。我希望有人能找到答案,因为我也需要它

我遇到了同样的问题,承诺在绑定到可观察对象的数据之前返回。我能想到的唯一“解决办法”是在数据访问层中使用JQuery,在数据绑定到可观察对象之后,绑定或执行我需要做的任何事情。 这是一个可怕的设计,违背了每一个代码分离,但我根本找不到另一种方法来让它工作

在本例中,我必须在数据绑定后调用change函数

代码如下所示:

呼叫虚拟机:

datacontext.getProduct(productObservable, productSearch.Id());
数据访问层(datacontext):

注意:

        ko.mapping.fromJS(data.results[0], {}, productObservable);
        // Should not be in here
        $('#productCategory').change();
$('#productCategory').change()正在触发数据绑定后需要触发的事件

因此,在本步骤中,您可以使用JQuery将可观察对象绑定到视图

同样,这是一个非常糟糕的设计,但我在这一点上找不到任何其他解决方案

希望这能帮助你至少让它发挥作用

        ko.mapping.fromJS(data.results[0], {}, productObservable);
        // Should not be in here
        $('#productCategory').change();