Skip to content

Instantly share code, notes, and snippets.

@hunterloftis
Created August 23, 2013 23:44
Show Gist options
  • Save hunterloftis/6325042 to your computer and use it in GitHub Desktop.
Save hunterloftis/6325042 to your computer and use it in GitHub Desktop.
dtvModule.directive('messagekey', function($http) {
var loadTimeout;
var keys = {};
return {
restrict: 'A',
link: link
};
function link(scope, el, attr) {
keys[attr.messagekey] = el;
// initially tried with $timeout, same results
clearTimeout(loadTimeout);
loadTimeout = setTimeout(onTimeout, 0);
// If this is here, it works
// otherwise, neither of the ajax calls are made
$http
.get('/api/messages', { params: { keys: _.keys(keys) } })
.success(function() {
console.log('wtf');
});
scope.$on('$destroy', onDestroy);
function onTimeout() {
console.log('timeout');
$http
.get('/api/messages', { params: { keys: _.keys(keys) } })
.success(onSuccess);
function onSuccess(data, status) {
console.log('success');
for (var key in data.strings) {
if (keys[key]) {
$(keys[key]).text(data.strings[key]);
delete keys[key];
}
}
}
}
function onDestroy() {
clearTimeout(loadTimeout);
keys = undefined;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment