2013-07-24 23 views
2

我正在开发一个JavaScript库和一个AngularJS前端。 JavaScript库需要是可移植的,所以它不能依赖于AngularJS。我们用一个相当标准的servlet查询模式:

queryService = function(url, method, params, resultHandler, queryId) 
{ 
    var request = { 
    jsonrpc: "2.0", 
    id: queryId || "no_id", 
    method: method, 
    params: params 
    }; 
    $.post(url, JSON.stringify(request), handleResponse, "json"); 

    function handleResponse(response) 
    { 
    if (response.error) 
     console.log(JSON.stringify(response, null, 3)); 
    else if (resultHandler) 
     resultHandler(response.result, queryId); 
    } 
}; 

queryService功能是通过在我们的库中的其他函数调用。你可以看到queryService什么都没有返回。它期望回调函数执行所有需要的操作。我不知道需要什么回调函数才能将结果返回到承诺对象的then()函数。这里的角服务代码:

angular.module("app").service("Data", function($q){ 
return { 
    getColNamesFromDb: function(table, scope){ 
    var deferred = $q.defer(); 

    var callbackFcn = function(result){ 
     console.log(result); // This successfully logs the result to the console! 
     deferred.resolve(result); // also tried deferred.resolve(); 
    }; 

    var safeApply = function(scope, fn) { 
     (scope.$$phase || scope.$root.$$phase) ? fn() : scope.$apply(fn); 
    }; 
    safeApply(scope, function(){ 
     deferred.resolve(queryWrapperForColNames(scope.tableName, callbackfcn)); 
     // also tried $q.when(queryWrapperForColNames(scope.tableName, callbackfcn)); 
    }); 

    return deferred.promise; 
    }}; 
}); 

从我的控制器我打电话Data.getColNamesFromDb(),并得到承诺的对象。但无论我尝试什么,我都无法让我的then()函数查看从数据库返回的内容。这里是控制器:

angular.module("app").controller("Ctrl", function($scope, Data) 
{ 
    $scope.options; 

    var promise = Data.getColNamesFromDb("table1", $scope); 

    promise.then(function(result){ 
     $scope.options = result; 
    },function(result){ 
     console.log("error " + result); 
    }); 
}) 

我知道我缺少有关的承诺是如何工作的一些愚蠢的事,但我什么也看不见。从我在代码中评论过的一些“选项”中应该可以清楚地看出,我只是在尝试使用随机方法并穿过我的手指。

回答

1

是尝试解决这个呼叫的第一件事:

var promise = Data.getColNamesFromDb("table1", $scope); 

根据你的榜样,该方法被定义为以$范围作为第一个参数,而不是第二个,所以你safeApply功能不会真正由于该字符串没有$$阶段属性,因此会导致发生$ digest循环。

编辑

现在的例子已得到纠正,试试这个:

angular.module("app").service("Data", function($q) { 
    return { 
     getColNamesFromDb: function(table, scope) { 
      var deferred = $q.defer(); 

      function safeApply(fn) { 
       if (!scope.$$phase) { 
        scope.$apply(fn); 
       } 
       else { 
        fn(); 
       } 
      } 

      queryWrapperForColNames(scope.tableName, function(result) { 
       safeApply(function() { 
        console.log(result); 
        deferred.resolve(result); 
       }); 
      }); 

      return deferred.promise; 
     } 
    }; 
}); 

在你原来的例子,你解决了两次推迟;一次在您的safeApply调用中,一次在您的回调函数中。延迟只能解析一次(进一步的调用没有效果),所以延迟将通过safeApply传递给它的值来解决。由于queryWrapperFor很可能隐含地返回undefined(我在这里没有看到这个定义),所以你的延期将被解析为undefined。

+0

你说得对。发布时,我意外地删除了'getColNamesFromDb()'的第一个参数。我通过添加一个'table'参数来纠正参数匹配。 – PatchCR

+0

谢谢!这工作!有一件事我不明白的是你的'safeApply'如何在不明确传入的情况下引用正确的'scope'。 – PatchCR

+0

'scope'变量已经被传递到getColNamesFromDb函数中,所以它在safeApply函数中可用,因为它仍然在函数的作用域中。 – dherman