1

我正在尝试在触发异步功能后关闭模式视图。异步函数是 BreezeJS。我使用的框架是 DurandalJS。

问题是我有一个输入字段,您可以在其中输入一个值。

如果这个值已经存在,那么我得到这个值,当您单击绑定到 OK 函数的按钮时,模式会很好地关闭。

但是,如果该值不存在,则需要创建它(在异步函数中获取)。从服务器返回调用后(哟,我创建了一个新实体,这是行的 ID),然后模式应该关闭,但是该部分(从异步函数关闭模式)不起作用。请提前帮助和感谢

define(function (require) {
    var dataservice = require('services/dataservice'),
        logger = require('services/logger'),
        isLoading = ko.observable(false),
        everythingFilledIn = ko.observable(true),
        containerNumberCorrect = ko.observable(true),
        customersRaw = ko.observableArray([]),
        containersRaw = ko.observableArray([]),
        billingCustomer = ko.observable(),
        customer = ko.observable(),
        container = ko.observable(),
        updaterTriggered = ko.observable(false),
        containerNumber = ko.observable();

    function init() {
        dataservice = new dataservice('api/data');

        dataservice.getAllRows('Customers').then(function (data) {
            isLoading(true);
            data.results.forEach(function (item) {
                customersRaw.push(item);
            });
            isLoading(false);
        }).fail(function () {
        });

        dataservice.getAllRows('Containers').then(function (data) {
            isLoading(true);
            data.results.forEach(function (item) {
                containersRaw.push(item);
            });
            isLoading(false);
        }).fail(function () {
        });
    }

    init();

    estimateStart = function (loggedInEmployee) {
        this.customers = function () {
            var customerName = [];
            mapCustomers = {};
            var data = customersRaw();
            $.each(data, function (i, customer) {
                mapCustomers[customer.Name()] = customer;
                customerName.push(customer.Name());
            });
            return customerName;
        };

        this.containers = function () {
            var containerNumber = [];
            mapContainers = {};
            var data = containersRaw();
            $.each(data, function (i, container) {
                mapContainers[container.ContainerNumber()] = container;
                containerNumber.push(container.ContainerNumber());
            });
            return containerNumber;
        };

        this.customer = customer;
        this.billingCustomer = billingCustomer;
        this.container = container;
        this.isLoading = isLoading;
        this.containerNumberCorrect = containerNumberCorrect;
        this.containerNumber = containerNumber;
        this.everythingFilledIn = everythingFilledIn;

        this.containerNumber.subscribe(function(newValue) {
            if (newValue.length < 13) {
                containerNumberCorrect(false);
                return;
            }
            else {
                checkContainerNumberFunction(newValue);
            }
        });



        /* //old this is for when I bind a css but that wont work because Bootstrap overwrites! Thats why the style binding in the html!
        this.containerDigitStatus = ko.computed(function () {
            return containerNumberCorrect() ? "containerNumberCorrect" : "containerNumberIncorrect";
        }, this);*/

        this.containerupdatefunction = function(item, element) {
            containerNumber(item);
            return item;
        };

        this.closeThisModal = function(jsonObj) {
            containerNumber("");
            billingCustomer(null);
            customer(null);
            container(null);
            updaterTriggered(false);
            this.modal.close(jsonObj);
        };

        this.ok = function() {
            if (this.containerNumberCorrect()) {
                try {
                    var metadataStore = dataservice.getMetadataStore();
                    var jsonObj = [];
                    var containerIDToUse = -999;
                    var goASynch = false;

                    if (container() == null) {
                        var type = metadataStore.getEntityType('tblContainer');
                        var newContainer = type.createEntity({ ContainerNumber: this.containerNumber() });
                        dataservice.manager.addEntity(newContainer);
                        dataservice.manager.saveChanges().then(function (data) {
                            jsonObj.push({
                                CustomerID: mapCustomers[customer()].CustomerID,
                                BillingCustomerID: mapCustomers[billingCustomer()].CustomerID,
                                ContainerID: data.entities[0].ContainerID()
                            });
                            containerNumber("");
                            billingCustomer(null);
                            customer(null);
                            container(null);
                            updaterTriggered(false);
                            this.self.close(jsonObj);// closeThisModal(jsonObj);
                        });
                        goASynch = true;
                    } else {
                        //container id van bestaande hier invoeren.
                        containerIDToUse = container().ContainerID();
                    }
                    if (!goASynch) {
                        jsonObj.push({
                            CustomerID: mapCustomers[this.customer()].CustomerID,
                            BillingCustomerID: mapCustomers[this.billingCustomer()].CustomerID,
                            ContainerID: containerIDToUse
                        });
                        this.closeThisModal(jsonObj);
                    }
                } catch (err) {
                    logger.logError("Error: " + err + "                    Possible unrecognized customers or containers!", null, null, true);
                }
            } else {
                logger.logError("The provide container number is NOT valid!", null, null, true);
            }
        };

        function checkContainerNumberFunction(containerNumberToCheck) {
            try {
                if (mapContainers[containerNumberToCheck].ContainerNumber() != undefined) {
                    container(mapContainers[containerNumberToCheck]);
                    containerNumberCorrect(true);
                    return;
                }
            } catch (err) {

            }

            if (containerNumberToCheck.trim().length == 13) {
                $.get(
                    "http://localhost:60312/api/baseapi/checkContainerDigit?digit=" + containerNumberToCheck,
                    function (data) {
                        if (data == true) {
                            container(null);
                            containerNumberCorrect(true);
                        } else {
                            containerNumberCorrect(false);
                        }
                        updaterTriggered(false);
                    }
                );
            } else {
                containerNumberCorrect(false);
                updaterTriggered(false);
            }
        };
    };

    estimateStart.prototype.updateViewAfterCustomerSelection = function (item) {
        customer(item);
        billingCustomer(item);
        return item;
    };
    /*
    estimateStart.prototype.ok = function () {
        if (this.containerNumberCorrect()) {
            try {
                var metadataStore = dataservice.getMetadataStore();
                var jsonObj = [];
                var containerIDToUse = -999;
                var goASynch = false;

                if (container() == null) {
                    var type = metadataStore.getEntityType('tblContainer');
                    var newContainer = type.createEntity({ ContainerNumber: this.containerNumber() });
                    dataservice.manager.addEntity(newContainer);
                    dataservice.manager.saveChanges().then(function(data) {
                        jsonObj.push({
                            CustomerID: mapCustomers[customer()].CustomerID,
                            BillingCustomerID: mapCustomers[billingCustomer()].CustomerID,
                            ContainerID: data.entities[0].ContainerID()
                        });
                        containerNumber("");
                        billingCustomer(null);
                        customer(null);
                        container(null);
                        updaterTriggered(false);
                        closeModalFunction(jsonObj);
                    });
                    goASynch = true;
                } else {
                    //container id van bestaande hier invoeren.
                    containerIDToUse = container().ContainerID();
                }
                if (!goASynch) {
                    jsonObj.push({
                        CustomerID: mapCustomers[this.customer()].CustomerID,
                        BillingCustomerID: mapCustomers[this.billingCustomer()].CustomerID,
                        ContainerID: containerIDToUse
                    });
                    this.closeThisModal(jsonObj);
                }
            } catch(err) {
                logger.logError("Error: " + err + "                    Possible unrecognized customers or containers!", null, null, true);
            }
        } else {
            logger.logError("The provide container number is NOT valid!", null, null, true);
        }
    };
    */
    /*
    estimateStart.prototype.closeThisModal = function(jsonObj) {
            containerNumber("");
            billingCustomer(null);
            customer(null);
            container(null);
            updaterTriggered(false);
            this.modal.close(jsonObj);
    };
    */
    estimateStart.prototype.closeModal = function () {
        updaterTriggered(false);
        return this.modal.close("close");
    };
    return estimateStart;
});

我也试过var self= this;,但这也行不通!

4

1 回答 1

0

我找到了答案。在工作时发布上述代码:在这种情况下将 varself = this;放入VMsoestimateStart = function (loggedInEmployee) {

define(function (require) {
    var dataservice = require('services/dataservice'),
        logger = require('services/logger'),
        isLoading = ko.observable(false),
        everythingFilledIn = ko.observable(true),
        containerNumberCorrect = ko.observable(true),
        customersRaw = ko.observableArray([]),
        containersRaw = ko.observableArray([]),
        billingCustomer = ko.observable(),
        customer = ko.observable(),
        container = ko.observable(),
        updaterTriggered = ko.observable(false),
        containerNumber = ko.observable();

    function init() {
        dataservice = new dataservice('api/data');

        dataservice.getAllRows('Customers').then(function (data) {
            isLoading(true);
            data.results.forEach(function (item) {
                customersRaw.push(item);
            });
            isLoading(false);
        }).fail(function () {
        });

        dataservice.getAllRows('Containers').then(function (data) {
            isLoading(true);
            data.results.forEach(function (item) {
                containersRaw.push(item);
            });
            isLoading(false);
        }).fail(function () {
        });
    }

    init();

    estimateStart = function (loggedInEmployee) {
        var self = this;

        this.customers = function () {
            var customerName = [];
            mapCustomers = {};
            var data = customersRaw();
            $.each(data, function (i, customer) {
                mapCustomers[customer.Name()] = customer;
                customerName.push(customer.Name());
            });
            return customerName;
        };

        this.containers = function () {
            var containerNumber = [];
            mapContainers = {};
            var data = containersRaw();
            $.each(data, function (i, container) {
                mapContainers[container.ContainerNumber()] = container;
                containerNumber.push(container.ContainerNumber());
            });
            return containerNumber;
        };

        this.customer = customer;
        this.billingCustomer = billingCustomer;
        this.container = container;
        this.isLoading = isLoading;
        this.containerNumberCorrect = containerNumberCorrect;
        this.containerNumber = containerNumber;
        this.everythingFilledIn = everythingFilledIn;

        this.containerNumber.subscribe(function(newValue) {
            if (newValue.length < 13) {
                containerNumberCorrect(false);
                return;
            }
            else {
                checkContainerNumberFunction(newValue);
            }
        });



        /* //old this is for when I bind a css but that wont work because Bootstrap overwrites! Thats why the style binding in the html!
        this.containerDigitStatus = ko.computed(function () {
            return containerNumberCorrect() ? "containerNumberCorrect" : "containerNumberIncorrect";
        }, this);*/

        this.containerupdatefunction = function(item, element) {
            containerNumber(item);
            return item;
        };

        this.closeThisModal = function(jsonObj) {
            containerNumber("");
            billingCustomer(null);
            customer(null);
            container(null);
            updaterTriggered(false);
            this.modal.close(jsonObj);

        };

        this.ok = function() {
            if (this.containerNumberCorrect()) {
                try {
                    var metadataStore = dataservice.getMetadataStore();
                    var jsonObj = [];
                    var containerIDToUse = -999;
                    var goASynch = false;

                    if (container() == null) {
                        var type = metadataStore.getEntityType('tblContainer');
                        var newContainer = type.createEntity({ ContainerNumber: this.containerNumber() });
                        dataservice.manager.addEntity(newContainer);
                        dataservice.manager.saveChanges().then(function (data) {
                            jsonObj.push({
                                CustomerID: mapCustomers[customer()].CustomerID,
                                BillingCustomerID: mapCustomers[billingCustomer()].CustomerID,
                                ContainerID: data.entities[0].ContainerID()
                            });

                        }).fin(function() {
                            containerNumber("");
                            billingCustomer(null);
                            customer(null);
                            container(null);
                            updaterTriggered(false);

                            //this.closeThisModal(jsonObj);
                            self.modal.close(jsonObj);// closeThisModal(jsonObj);
                        });
                        goASynch = true;
                    } else {
                        //container id van bestaande hier invoeren.
                        containerIDToUse = container().ContainerID();
                    }
                    if (!goASynch) {
                        jsonObj.push({
                            CustomerID: mapCustomers[this.customer()].CustomerID,
                            BillingCustomerID: mapCustomers[this.billingCustomer()].CustomerID,
                            ContainerID: containerIDToUse
                        });
                        this.closeThisModal(jsonObj);
                    }
                } catch (err) {
                    logger.logError("Error: " + err + "                    Possible unrecognized customers or containers!", null, null, true);
                }
            } else {
                logger.logError("The provide container number is NOT valid!", null, null, true);
            }
        };

        function checkContainerNumberFunction(containerNumberToCheck) {
            try {
                if (mapContainers[containerNumberToCheck].ContainerNumber() != undefined) {
                    container(mapContainers[containerNumberToCheck]);
                    containerNumberCorrect(true);
                    return;
                }
            } catch (err) {

            }

            if (containerNumberToCheck.trim().length == 13) {
                $.get(
                    "http://localhost:60312/api/baseapi/checkContainerDigit?digit=" + containerNumberToCheck,
                    function (data) {
                        if (data == true) {
                            container(null);
                            containerNumberCorrect(true);
                        } else {
                            containerNumberCorrect(false);
                        }
                        updaterTriggered(false);
                    }
                );
            } else {
                containerNumberCorrect(false);
                updaterTriggered(false);
            }
        };
    };

    estimateStart.prototype.updateViewAfterCustomerSelection = function (item) {
        customer(item);
        billingCustomer(item);
        return item;
    };
    /*
    estimateStart.prototype.ok = function () {
        if (this.containerNumberCorrect()) {
            try {
                var metadataStore = dataservice.getMetadataStore();
                var jsonObj = [];
                var containerIDToUse = -999;
                var goASynch = false;

                if (container() == null) {
                    var type = metadataStore.getEntityType('tblContainer');
                    var newContainer = type.createEntity({ ContainerNumber: this.containerNumber() });
                    dataservice.manager.addEntity(newContainer);
                    dataservice.manager.saveChanges().then(function(data) {
                        jsonObj.push({
                            CustomerID: mapCustomers[customer()].CustomerID,
                            BillingCustomerID: mapCustomers[billingCustomer()].CustomerID,
                            ContainerID: data.entities[0].ContainerID()
                        });
                        containerNumber("");
                        billingCustomer(null);
                        customer(null);
                        container(null);
                        updaterTriggered(false);
                        closeModalFunction(jsonObj);
                    });
                    goASynch = true;
                } else {
                    //container id van bestaande hier invoeren.
                    containerIDToUse = container().ContainerID();
                }
                if (!goASynch) {
                    jsonObj.push({
                        CustomerID: mapCustomers[this.customer()].CustomerID,
                        BillingCustomerID: mapCustomers[this.billingCustomer()].CustomerID,
                        ContainerID: containerIDToUse
                    });
                    this.closeThisModal(jsonObj);
                }
            } catch(err) {
                logger.logError("Error: " + err + "                    Possible unrecognized customers or containers!", null, null, true);
            }
        } else {
            logger.logError("The provide container number is NOT valid!", null, null, true);
        }
    };
    */
    /*
    estimateStart.prototype.closeThisModal = function(jsonObj) {
            containerNumber("");
            billingCustomer(null);
            customer(null);
            container(null);
            updaterTriggered(false);
            this.modal.close(jsonObj);
    };
    */
    estimateStart.prototype.closeModal = function () {
        updaterTriggered(false);
        return this.modal.close("close");
    };
    return estimateStart;
});
于 2013-05-16T09:55:04.650 回答