0

我有一个带有多个下拉菜单的屏幕。每个都通过 web api 调用填充。我在一个活动目录环境中。我正在使用 Durandal SPA 框架。

在此处输入图像描述

目前我在我的视图激活时加载我的下拉列表。例如。

  activate: function (data) {
            var id = data.id || undefined;
            userRole(data.mode || 'usr');

            // i added this as i was thinking i dont need to initialize the "static" dropdowns every time
            // I'm testing my code without this as we speak.
            if (!hasBeenInit) {
                lookupInit(id);
                hasBeenInit = true;
            }
        },
   }

[A] 这似乎是随机的,取决于服务器负载量(我猜)我的下拉菜单不会被填充..

[B] 一些下拉菜单依赖于另一个下拉菜单的值。我已经通过使用淘汰订阅事件来实现这一点。并更新我的可观察集合。

Javascript

userInfo.BuildingCode.subscribe(function (value) {
        vm.userInfo.FloorCode(undefined);
        http.get('api/Floor', { id: value }, false)
            .done(function (response) {
                console.log('api/Floor');
                vm.lookups.allFloors(response);
                console.log(vm.lookups.allFloors());
            });
    });

html

<select data-bind="options: lookups.allFloors, optionsText: 'FloorName', optionsValue: 'FloorCode', value: $root.userInfo.FloorCode"></select>

例如,如果我必须选择建筑物值,则楼层下拉列表必须获得新值。API调用确实发生了,似乎可观察数组得到了更新,但这些值似乎没有反映在视图上。

Chrome 控制台日志

api/Floor main-built.js:14
[Object, Object, Object]

api/Floor main-built.js:14
[Object, Object, Object]

api/Floor main-built.js:14
[Object, Object, Object, Object, Object]

我能做些什么来确保每次调用都填充我的查找/下拉列表。以及它们的执行顺序保持指定。

我试过这样的事情,但感觉不对。我还向 durandal http 帮助程序添加了一个异步错误重载,但它似乎没有用......有什么想法吗?:\

   http.get('api/employmenttype')
            .done(function (response) {
                console.log('api/employmenttype');
                vm.lookups.allEmploymentTypes(response);
            })
            .then(function () {
                http.get('api/actionlist')
                    .done(function (response) {
                        console.log('api/actionlist');
                        vm.lookups.allActionListOptions(response);
                    })
                    .then(function () {  ... } );

更新

我调查了我的异步调用过载。最终成为罪魁祸首,一旦我修复它,我的数据似乎不断加载。唯一的副作用是我的服务器端数据集都没有异步加载。(我希望我不会被钉在十字架上)。

4

1 回答 1

1

Assuming that lookupInit is a async call make sure to return the promise. So activate becomes something along the line.

activate: function (data) {
    var id = data.id || undefined;
    userRole(data.mode || 'usr');

    // i added this as i was thinking i dont need to initialize the "static" dropdowns every time
    // I'm testing my code without this as we speak.
    if (!hasBeenInit) {
        hasBeenInit = true;
        return lookupInit(id);
    }
    return true;
},

I'm still digesting the other part(s) of the question, but thought that might get you started already.

Update: The value of vm.userInfo.FloorCode is reset to undefined, but in the select there's no optionsCaption: 'Please select...' defined. It might be that ko won't update the option values because is can't find a corresponded value.

于 2013-07-17T10:13:31.040 回答