Skip to content

Instantly share code, notes, and snippets.

@joeylin
Created November 24, 2013 14:22
Show Gist options
  • Save joeylin/7627813 to your computer and use it in GitHub Desktop.
Save joeylin/7627813 to your computer and use it in GitHub Desktop.
some common codes used in every third part merged angular plugin
// Garbage collection
// below example
// change third plugin to angular service
// used in most station
scope.$on('$destroy', function() {
$modal.remove();
});
// Emit modal events
// map all third plugin event to angular scope
// so other angular plugins can interact with it
angular.forEach(['show', 'shown', 'hide', 'hidden'], function(name) {
$modal.on(name + evSuffix, function(ev) {
scope.$emit('modal-' + name, ev);
});
});
// map plugin public method to scope
// this scope can be configed, default to rootscope
angular.forEach(['show', 'hide'], function(name) {
scope[name] = function() {
$modal.modal(name);
};
});
// use angular factory method to create service
// this way returns a function, so we can pass in a config object
// when need ajax load template, can use below way to complete
return $q.when($templateCache.get(templateUrl) || $http.get(templateUrl, {cache: true})
.then(function(res) { return res.data;} }))
.then(function onSuccess(template) {
// our plugin code excutes when template has loaded
}
// Compile modal content
$timeout(function() {
$compile($modal)(scope);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment