-
-
Save ryardley/176fa71b07ea18110b73 to your computer and use it in GitHub Desktop.
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('utils.ElementRegistryDemo',[]) | |
.directive('elem', function(ElementRegistry) { | |
return { | |
restrict: 'A', | |
scope: false, | |
link:function(scope, element, attrs){ | |
// store this element with reference to the parent scope | |
new ElementRegistry(scope).add(attrs.elem, element); | |
} | |
}; | |
}) | |
.factory('ElementRegistry', function () { | |
// scope id to store on the class | |
var id; | |
function ElementRegistry(scope){ | |
// initialize class stores if they are undefined | |
id = scope.$id; | |
ElementRegistry.store = ElementRegistry.store || {}; | |
ElementRegistry.store[id] = ElementRegistry.store[id] || {}; | |
} | |
// get a key from the store | |
function findByKey(key){ | |
return ElementRegistry.store[id][key] ? ElementRegistry.store[id][key] : false; | |
} | |
ElementRegistry.prototype = { | |
// return the found value otherwise run a callback once found | |
find: function(key, callback){ | |
return findByKey(key); | |
}, | |
// add an element to the scope store | |
add: function(key, value){ | |
ElementRegistry.store[id][key] = value; | |
}, | |
// return the storage object | |
store: function(){ | |
return ElementRegistry.store[id]; | |
} | |
}; | |
return ElementRegistry; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment