Created
August 23, 2013 23:44
-
-
Save hunterloftis/6325042 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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