Created
August 20, 2014 03:55
-
-
Save idleworx/c18ce3dc2c4acb3de1bc to your computer and use it in GitHub Desktop.
AngularJS MessageService.js Usage Example 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//angular.module('yourappmodule',['idleworx-message-service']); //don't forget to add a dependency to your apps's module | |
angular.module('idleworx-message-service') | |
.factory('MessageService',['$log',function ($log) { | |
$log.info('MessageService Initialized ...'); | |
var service = { | |
messages:{},//object containing different types of messages, for each scope | |
/** service.init() must be called before logging to a scope | |
* Typically this is done at the beginning of your controller code */ | |
init:function(scope){ | |
service.initService(scope); | |
service._watchForMessage(scope); | |
}, | |
/** | |
* Sets up the service.messages object, gives it an entry with a key of the current scope | |
* This allows us to call MessageService.init($scope) on any passed in scope | |
*/ | |
initService:function(scope){ | |
service.messages[service._simplifyScope(scope)] = | |
{ | |
scope:service._simplifyScope(scope), | |
infoMessage:null, | |
warnMessage:null, | |
successMessage:null, | |
errorMessage:null, | |
debugMessage:null, | |
debugData:null | |
}; | |
}, | |
info:function(scope,message){ | |
service.messages[service._simplifyScope(scope)].infoMessage = message; | |
}, | |
warn:function(scope,message){ | |
service.messages[service._simplifyScope(scope)].warnMessage = message; | |
}, | |
success:function(scope,message){ | |
service.messages[service._simplifyScope(scope)].successMessage = message; | |
}, | |
error:function(scope,message){ | |
service.messages[service._simplifyScope(scope)].errorMessage = message; | |
}, | |
debug:function(scope,message,data){ | |
service.messages[service._simplifyScope(scope)].debugMessage = message; | |
service.messages[service._simplifyScope(scope)].debugData = data; | |
}, | |
clear:function(scope){ | |
service.messages[service._simplifyScope(scope)] = { | |
infoMessage:null, | |
warnMessage:null, | |
successMessage:null, | |
errorMessage:null, | |
debugMessage:null, | |
debugData:null | |
}; | |
}, | |
/** Adds a watch function to the passed in scope that watches for updates to the messages object | |
* This allows the calling object (eg. controller) to get updates on when the messages object changes | |
*/ | |
_watchForMessage:function(scope){ | |
scope.$watch( function () { return service.messages[service._simplifyScope(scope)]; }, function ( m ) { | |
$log.warn('$watch','watchForMessage()',m); | |
if(!_.isEmpty(m)) | |
scope.messages = m; | |
else | |
scope.messages = null; | |
},true); | |
}, | |
/** Because scope is an object, we can simply use the scope's $id property as a key | |
* Instead of the whole object | |
* WARNING: This means you MUST pass in a valid AngularJS scope or you will get an error */ | |
_simplifyScope:function(scope){ | |
return JSON.stringify(scope.$id); | |
} | |
}; | |
return service; | |
}]) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment