Skip to content

Instantly share code, notes, and snippets.

@dimified
Created April 16, 2016 15:42
Show Gist options
  • Save dimified/bb2e8717e68809df7f90f21995c063e7 to your computer and use it in GitHub Desktop.
Save dimified/bb2e8717e68809df7f90f21995c063e7 to your computer and use it in GitHub Desktop.
Service: Messaging (Service for handling messages)
'use strict'
define [
'jquery'
'angular'
], ($, Angular) ->
###*
# Registry of message service
# @namespace App.services.messageSrvc
# @memberof App.services
###
Angular.module 'App.services.messageSrvc', []
.service 'message', [ () ->
###
# Manage and display messages which are thrown from application as errors.
# All messages are displayed in the proper area under the header.
###
###*
# Object with several types of message arrays
# @property {object} messages
# @memberof App.services.messageSrvc
###
messages =
alert: []
success: []
info: []
warning: []
{
###*
# Object with several types of message arrays
# @property {function}
# @memberof App.services.messageSrvc
###
messages: messages
###*
# Toggle message by showing the contents of messages inside the alert viewlet
# @function
# @memberof App.services.messageSrvc
# @param {string} type Type of message
###
toggle: (type) ->
$('#alertViewlet .' + type).show()
return
###*
# Pop message specifically and hide viewlet when messages is empty
# @function
# @memberof App.services.messageSrvc
# @param {string} type Type of message
# @return {number}
###
pop: (type) ->
messages[type].pop()
if messages[type].length is 0
$('#alertViewlet .' + type).hide()
messages[type].length
###*
# Push message with type and message field being showed in message viewlet
# @function
# @memberof App.services.messageSrvc
# @param {string} type Type of message
# @param {string} message Message
# @return {number}
###
push: (type, message) ->
if messages[type].indexOf(message) < 0
messages[type].push(message)
this.toggle(type)
messages[type].length
###*
# Clear message container, hide the viewlet and return number
# @function
# @memberof App.services.messageSrvc
# @param {string} type Type of message
# @return {number}
###
clear: (type) ->
$('#alertViewlet .' + type).hide()
messages[type] = []
messages[type].length
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment