Skip to content

Instantly share code, notes, and snippets.

@asus4
Created August 4, 2014 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asus4/da257c12bffd1b6ce747 to your computer and use it in GitHub Desktop.
Save asus4/da257c12bffd1b6ce747 to your computer and use it in GitHub Desktop.
Simple sound effect module for AngularJS
# This is Simple Audio Plugin for AngularJS
# service
class AngularAudio
constructor:() ->
@cache = {}
play:(id) ->
audio = null
if (@cache[id]) # check cache
audio = @cache[id]
else # load from DOM
audio = document.querySelector(id)
@cache[id] = audio
if audio
@playSE(audio)
playSE:(audio) ->
audio.load()
audio.play()
#================
# Assign modules
#================
_module = angular.module('ngAudio', [])
_module.directive('ngAudio', ($compile, ngAudio) ->
return {
restrict: 'AE'
controller: ($scope, $attrs, $element) ->
$element.on('click', (e) ->
ngAudio.play($attrs.ngAudio)
)
}
)
_module.service('ngAudio', AngularAudio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment