1

編集:関数 populateDropdown および関数 isSystemCorrect のコードを追加しました (下部を参照)

編集2少し絞り込みましたが、計算されたオブザーバブルのarrayFilter関数で問題が発生したようです。これは、何を試しても空の配列を返します。フィルタリングの直前に self.testsuites() が正常に見えることを確認しましたが、フィルタリングはまだ失敗します。

計算されたオブザーバブル、filteredTestsuites に問題があります。

スクリーンダンプからわかるように、テストスイートのオブザーバブルは正しく設定されていますが、計算されたオブザーバブルは空のままです。また、ドロップダウンメニューから「支払い」以外のオプションを選択しようとしましたが、これがオブザーバブルをトリガーするかどうかを確認しましたが、そうではありませんでした。

self.testsuites() または self.dropdownSelected() が変更されるたびに、計算されたオブザーバブルが更新されると思いますが、どちらでもトリガーされないようです。

ここで何が間違っていますか?

どちらかが変更されるたびに、選択したドロップダウンオプションの後に、計算されたオブザーバブルフィルターをテストスイートにしたいだけです。

コンソールからのスクリーンダンプ

function ViewModel() {
    var self = this;

    // The item currently selected from a dropdown menu
    self.dropdownSelected = ko.observable("Payment");

    // This will contain all testsuites from all dropdown options
    self.testsuites = ko.mapping.fromJS('');

    // This will contain only testsuites from the chosen dropdown option
    self.filteredTestsuites = ko.computed(function () {
        return ko.utils.arrayFilter(self.testsuites(), function (testsuite) {
            return (isSystemCorrect(testsuite.System(), self.dropdownSelected()));
        });
    }, self);

    // Function for populating the testsuites observableArray
    self.cacheTestsuites = function (data) {
        self.testsuites(ko.mapping.fromJS(data));
    };


    self.populateDropdown = function(testsuiteArray) {

        for (var i = 0, len = testsuiteArray().length; i < len; ++i) {

            var firstNodeInSystem = testsuiteArray()[i].System().split("/")[0];

            var allreadyExists = ko.utils.arrayFirst(self.dropdownOptions(), function(option) {
                return (option.Name === firstNodeInSystem);
            });

            if (!allreadyExists) {
                self.dropdownOptions.push({ Name: firstNodeInSystem });
            }
        }
    };
}


$(document).ready(function () {

    $.getJSON("/api/TestSuites", function (data) {
        vm.cacheTestsuites(data);
        vm.populateDropdown(vm.testsuites());
        ko.applyBindings(vm);
    });
}

関数 isSystemCorrect:

function isSystemCorrect(system, partialSystem) {

    // Check if partialSystem is contained within system. Must be at beginning of system and go
    // on to the end or until a "/" character.
    return ((system.indexOf(partialSystem) == 0) && (((system[partialSystem.length] == "/")) ||     (system[partialSystem.length] == null)));
}
4

1 に答える 1