2

我未能实现我的 REST 资源和 AngluarJS 日历控件之间的绑定。似乎我遗漏了一些东西,或者只是没有从逻辑上理解它,因为只要有单独的组件,所有组件都在工作。我用这个 Plunker 作为日历实现的例子:http ://plnkr.co/edit/VbYDNK?p=preview

视图内部:

...
<div ng-controller="CalendarCtrl" dev-calendar="uiConfig.calendar" class="span8 calendar" ng-model="eventSources" style="padding-top: 30px;"></div>
...

控制器内部:

     xyz.controller('MainController', function($scope, $location, $rootScope, MainModel) {
        ...
        // REST call - getPendingObjectsByUser
            $scope.pendingobjects = MainModel.pendingObjectsByUser.get(
                    {userId : $rootScope.currentUser.userId}, 
                        function(response) {                    

                            // do something                                         

                    }, function(response) {

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


                    });
    // this works fine              
    ...
    function CalendarCtrl($scope) {

            var date = new Date();var d = date.getDate();var m = date.getMonth();var y = date.getFullYear();

                $scope.events = [{type:'birthday', title: 'Brian',start: new Date(y, m, 1)}];

                $scope.eventSources = [$scope.events];

                $scope.uiConfig = {
                  calendar:{
                    height: 450, editable: false, defaultView: 'month'
                  }
                };
        }
// this also works fine

现在我正在尝试做一个简单的绑定,例如:

 $scope.events = [{title: $scope.pendingobjects.objectsData[0].title , type:'birthday', start: new Date(y, m, 1)}]

但由于异步行为,它的“未定义”。如果我将整个 $scope.events 放入可用的响应函数中(如下所示),则日历不再起作用。

 // getPendingObjectsByUser
            $scope.pendingobjects = MainModel.pendingObjectsByUser.get(
                    {userId : $rootScope.currentUser.userId}, 
                        function(response) {                    

                            // do something
                            $scope.events = [{type:'birthday', title:'$scope.pendingobjects.objectsData[0].title',start: new Date(y, m, 1)}]                                                                                        

 }, function(response) {

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

                    })

我是否需要与承诺一起工作,或者我只是对自己的错误感到困惑?

4

1 回答 1

1

为什么不立即将事件绑定到资源?像这样:

$scope.events = function (start, end, callback) {
  var events = MainModel.pendingObjectsByUser.get({
    userId : $rootScope.currentUser.userId
  });

  // Promise 
  events.$promise.then(function (val) {
          $scope.events = events;
          callback($scope.events);
      }
  );
};
      $scope.eventSources = [$scope.events];
于 2013-11-28T11:09:50.637 回答