Sha256: e654573a6c1fc3ada430ab5c5d0e2922e3dc53feb582d68a0218cf354e0ef770

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

angular.module "mb.services", [], ($provide) ->

  $provide.factory "alerts", [
    "$log", "$timeout", ($log, $timeout) ->
      lastId: 0
      messages: []

      # Returns a next id for the new message
      nextId: ->
        @lastId += 1

      push: (type, text) ->
        id = @nextId()
        $log.info("Alert [#{id}, #{type}]", text)

        @messages.push(id: id, type: type, text: text)
        @delayedDispose(id)

        id

      # Helper methods for various alerts types
      info: (text) -> @push("info", text)
      error: (text) -> @push("error", text)

      # Disposes a message with the given id
      dispose: (id) ->
        at = @messages.map((message) -> message.id).indexOf(id)
        @messages.splice(at, 1)

      # Dispose the message after the given time in milliseconds
      delayedDispose: (id, timeout = 3000) ->
        disposeTheAlert = =>
          $log.info("Disposing alert", id, "after", timeout, "milliseconds")
          @dispose(id)
        $timeout(disposeTheAlert, timeout)
  ]

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo_browser-0.2.0.rc2 app/assets/javascripts/app/services.js.coffee