1

我正在尝试将一个非结构化的 jQuery 应用程序转换为 MVC AngularJS,但有一次我有点迷路或者我只是不明白......(不幸的是,我不是 JavaScript 之神,所以错误也可能存在)

这是原始 jQuery 代码的片段。

$.ajax({
type: "GET",  
    url: "rest/users/" + userId + "/requests",  
                accept: "application/json; charset=utf-8",
                statusCode: {
                    200: function(data) {               
                        // do something
                    },
                    404: function() {
                        window.location = 'index.html';
                    },
                    500: function() {
                        alert("Server Error!");
                    }
                }                   
            });

一个简单的 REST 调用,其中使用 HTTP 响应代码进行导航。不幸的是,我无法在 AngularJS 中运行它。

这是我的控制器:

// Inside the RequestController
$scope.requests = RequestModel.getRequestsByUser($rootScope.currentUser.userId);  
    if($scope.requests.statusCode == 200) { 
        // do something         
    }
    else if($scope.requests.statusCode == 404) {
        $location.path('/notFound');
    } else if ($scope.requests.statusCode == 500) {
        $location.path('/error');
    }; // PROBLEM: The if/else statement is never true since statusCode is not available

这是我的模型:

// Inside the RequestModel
this.getRequestsByUser = function(userId) {

    var RequestResource = $resource('../rest/users/' + userId + "/requests");
    var requestList = RequestResource.get({}, function(response, getResponseHeaders) {

        // PROBLEM: The property "stausCode" is "unavilable" at the Controller even if it was set here
        requestList.statusCode = 200;
        console.log("SUCCESS: getRequestsByUser() -> StatusCode: requestList.statusCode");
        console.log(requestList);

    }, function(response, getResponseHeaders) {

        requestList.statusCode = response.status;
        console.log("FAILED: getRequestsByUser() -> StatusCode: " + response.status);

    });
    return requestList;
};

这不起作用,因为“statusCode”在我的控制器中“不可用”。REST 调用有效,并且数据绑定到视图也很好。我只是无法实现“导航部分”。我是否错过了诸如 $watch 属性、异步行为之类的东西,还是我的方法不正确?!

谢谢你的帮助!

4

1 回答 1

2

You can make better use of resource parameter mapping in your service:

// Inside service
this.requestsByUser = $resource('../rest/users/:userId/requests', {userId:'@userId'});

That way you'll be able to reuse the same resource for different rest actions (eg. post, delete).

And controller code to handle statuses (response handlers were moved to controller):

// Inside the RequestController
$scope.requests = RequestModel.requestsByUser
    .get(
      {userId: $rootScope.currentUser.userId},

      function(response) { // success handler
          if(response.status == 200) {
              // do something         
          }
      },

      function(response) { // error handler
          if(response.status == 404) {
              $location.path('/notFound');
          } else if (response.status == 500) {
              $location.path('/error');
          }
      }
    );

Another way around is to use $q service to return promises from your service. But provided solutions seems cleaner to me

于 2013-04-09T08:46:02.413 回答