durandal激活函数异步代码

时间:2013-08-08 09:17:48

标签: javascript durandal

我正在使用Durandal 1.2,在我的viewmodel中我有功能激活:

    self.activate = function (data) {
        function update(clienteId) {
            self.Loading(true);
            return $.when(
               clienteRepository.ClienteCompact(clienteId),
               clienteRepository.Prodotti(clienteId),
               clienteRepository.Interventi(clienteId),
               clienteRepository.Abbonamenti(clienteId)
               ).then(function (cliente, prodotti, interventi, abbonamenti) {
                   self.Cliente(cliente[0]).Prodotti(prodotti[0]).Interventi(interventi[0]).Abbonamenti(abbonamenti[0]);
                   self.Loading(false);
                   debugger;
               });
        };

        var clienteId = data.splat[1];
        if ($.isNumeric(clienteId)) {
            return update();
        } else {
            return true;
        }
    };

我有功能beforeBind:

    self.beforeBind = function () {
        debugger;
    };

问题是在ajax请求结束之前调用函数beforeBind,因此视图为空。

有什么问题?

谢谢;)

1 个答案:

答案 0 :(得分:0)

只需在then()语句中移动函数中的代码即可。 then()处理来自Ajax的返回的promise。

另一种选择是将函数从构造函数更改为标准函数语句。就像你的函数在被调用之前一样被评估。

而不是

var myFunc = function () {};

使用

function myFunc () {} 
相关问题