AngularUI日历事件源,在每个视图切换

时间:2015-10-28 13:34:55

标签: angularjs fullcalendar angular-ui

为了证明这个问题,我将解释我想要实现的目标。我正在使用AngularUI日历创建一个预订系统,问题是当事件数量增加时(100多个事件,日历将需要几分钟),日历的性能开始迅速下降绑定数据)。由于我每次用户更改视图(周,月,日,上一页,下一行等等)时都通过发送AJAX请求来尝试从后端获取数据,这是我的时间开始有问题。

我在AngularUI Calendar网站上建议我可以使用此功能:$scope.eventsF = function (start, end, timezone, callback) {...}并将其添加到$scope.eventSources,并在每个视图切换时调用它。我已将$scope.eventsF添加到我的$scope.eventSources,但它从未更新。

以下是示例(获取所有数据,并且它正在运行):

$scope.mainEvents = []; //To initiate the event source
Event.getEvents().then(function (events) { // Event is a factory
  if (events) { //Checking if I have events
    // If I try: $scope.mainEvents = events; the $scope.eventSources won't update
    angular.forEach(events, function(value) {
      this.push(value);
    }, $scope.mainEvents);
  } else {
    // Tell the user no events to render...
  }
}, function (error) {
  console.log(error);
});

$scope.eventSources = [$scope.mainEvents, $scope.draftEvents];

现在我更改Event.getEvents()以获取两个参数作为视图的开始日期和结束日期,将它们发送到后端,并再次以json的形式检索数据(工作正常,我安慰了它)。 / p>

以下是代码(我尝试了两种方式):事件正在呈现,但$scope.eventSources并未更新。

首先:

$scope.mainEvents = function (start, end, timezone, callback) {
  var s = start.format('YYYY-MM-DD'),
      e = end.format('YYYY-MM-DD');
  Event.getEvents(s, e).then(function (events) {
    $scope.mainEvents = []; //even tried $scope.eventSources.mainEvents
    if (events) {
      angular.forEach(events, function (value) {
        this.push(value);
      }, $scope.mainEvents); //event tried $scope.eventSources.mainEvents
      callback($scope.mainEvents);
    }
  }, function (error) {
    console.log(error);
  });
};

$scope.eventSources = [$scope.mainEvents, $scope.draftEvents];

第二:

$scope.eventsF = function (start, end, timezone, callback) {
  var s = start.format('YYYY-MM-DD'),
      e = end.format('YYYY-MM-DD');
  Event.getEvents(s, e).then(function (events) {
    $scope.mainEvents = [];
    if (events) {
      angular.forEach(events, function (value) {
        this.push(value);
      }, $scope.mainEvents);
      callback($scope.mainEvents);
    }
  }, function (error) {
    console.log(error);
  });
};

$scope.eventSources = [$scope.mainEvents, $scope.draftEvents, $scope.eventsF];

我希望这很清楚。

3 个答案:

答案 0 :(得分:1)

我能想到的最简单的方法是在$scope.eventSources结束时将事件来源重新分配到$scope.eventsF。以下代码基于您的第二个选项。欢呼声。

$scope.mainEvents = [];
$scope.draftEvents = [];
$scope.eventsF = function (start, end, timezone, callback) {
    var s = start.format('YYYY-MM-DD'),
    e = end.format('YYYY-MM-DD');
    Event.getEvents(s, e).then(function (events) {
        $scope.mainEvents = [];
        if (events) {
            angular.forEach(events, function (value) {
                this.push(value);
            }, $scope.mainEvents);
            // Re-assign event sources
            $scope.eventSources = [$scope.mainEvents, $scope.draftEvents, $scope.eventsF];
            // Assume that 'events' is an array with 5 elements.
            // console.log($scope.eventSources) will produce :
            // Array [ Array[5], Array[0], app</$scope.eventsF() ]

            callback($scope.mainEvents);
        }
    }, function (error) {
        console.log(error);
    });
};
$scope.eventSources = [$scope.mainEvents, $scope.draftEvents, $scope.eventsF];
// console.log($scope.eventSources) will produce :
// Array [ Array[0], Array[0], app</$scope.eventsF() ]

修改

我已更新上面的代码,以显示我测试$scope.eventSources的哪个部分。您能否在$scope.eventSources获取更新的示例代码的哪一部分显示?

答案 1 :(得分:1)

我猜它是由Javascript的变量范围引起的。您引用的$scope.eventSources范围与控制器中的$scope.eventSources不同。也许$scope.eventSources会被AngularUI Calendar中的某处复制。

您只需创建一个包含变量引用的变量,即可访问控制器中的$scope.eventSources

// Create a variable that holds reference to controller's $scope
var ctrlScope = $scope;

$scope.mainEvents = [];
$scope.draftEvents = [];
$scope.eventsF = function (start, end, timezone, callback) {
    var s = start.format('YYYY-MM-DD'),
    e = end.format('YYYY-MM-DD');
    Event.getEvents(s, e).then(function (events) {
        if (events) {
            angular.forEach(events, function (value) {
                this.push(value);
            }, $scope.mainEvents);
            // Test
            console.log(ctrlScope.eventSources);

            callback($scope.mainEvents);
        }
    }, function (error) {
        console.log(error);
    });
};
$scope.eventSources = [$scope.mainEvents, $scope.draftEvents, $scope.eventsF];

答案 2 :(得分:1)

我找到了问题的解决方案。我不明白为什么我必须这样做,但我设法让它工作。我已逐个从$scope.mainEvents数组中删除事件(我认为在AngularUI日历中,它们对每个事件使用$watch),然后将新事件从promise添加到{{ 1}}也是一个接一个。 不要使用$scope.mainEvents功能,因为callback()会更新($scope将负责渲染),因此$watch会再次渲染事件,这样您最终会将每个事件渲染两次。 这是最终的代码:

callback()