Angular.js 2个服务的循环依赖

时间:2015-01-30 13:00:04

标签: angularjs

解决StatusTrackerSvc.callNewEvent的循环依赖关系的最简洁方法是什么?我真的想把这两个服务文件分开。

angular.module("main.loadbalancer").factory "StatusTrackerSvc", (
  StatusSvc
  IteratorSvc
) ->

  runEventCheck = (eventId, modalInstance) ->
    IteratorSvc.callNewEvent(eventId, 0).then(->
      StatusSvc.eventCheckSuccess modalInstance
    , (reason) ->
      StatusSvc.eventCheckFailure reason
    )

  eventCheckStart: StatusSvc.eventCheckStart
  runEventCheck: runEventCheck
  eventCheckResetValues: StatusSvc.eventCheckResetValues

angular.module("main.loadbalancer").service "IteratorSvc", (
  $injector
  $timeout
  ErrorSvc
  $q
  EventSvc
  Configuration
  StatusTrackerSvc
) ->

  checkStatus = (data) ->
    data.Automation.Status isnt "FAILED" and data.Automation.Status isnt "COMPLETEDWITHERROR"

  callNewEvent = (eventId, iteration)  ->
    check = (data, eventId, deferred, iteration) ->
      if iteration is Configuration.CHECK_ITERATIONS
        deferred.reject "The maximum #{Configuration.CHECK_ITERATIONS}
        attempts have been reached in checking status."
      else if checkStatus(data)
        iteration++
        $timeout (->
          StatusTrackerSvc.callNewEvent eventId, iteration
        ), Configuration.TIME_ITERATION
      else
        errorMessage = ErrorSvc.nonSuccess(data)
        deferred.reject errorMessage

    url = EventSvc.getEvent(eventId)
    deferred =  $q.defer()
    url.get().then (data) ->
      if data.Automation.Status isnt "COMPLETED"
        check data, eventId, deferred, iteration
      else
        deferred.resolve "complete"
      deferred.promise



  callNewEvent: callNewEvent

1 个答案:

答案 0 :(得分:1)

angular.module("main.loadbalancer").factory "StatusTrackerSvc", (
  StatusSvc
  IteratorSvc
)

angular.module("main.loadbalancer").service "IteratorSvc", (
  $injector
  $timeout
  ErrorSvc
  $q
  EventSvc
  Configuration
  StatusTrackerSvc

有你的问题“IteratorSvc”需要“StatusTrackerSvc”并且他需要“IteratorSvc”,如果你需要共同日期,你就不应该拥有它,把它放在应该这样做的第三个服务中