Skip to content

Commit

Permalink
Add a loadAngular function
Browse files Browse the repository at this point in the history
    If something is added to the DOM with external tools, angular is not
aware of these changes (Ajax call, pure js DOM manipulation, etc...).
This patch add an utility function which will load angular on the given
element.
    There is also an example on modal, angular is now loaded when a
modal is shown.

Fixes bug #1258192

Change-Id: I3266b455d79d7d1856e4a622e927ed4af8e90b5e
  • Loading branch information
Maxime Vidori committed Dec 9, 2013
1 parent efc88d4 commit 4b528b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions horizon/static/horizon/js/horizon.modals.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ horizon.addInitFunction(function() {
$this = $(this);
modal_stack.splice(modal_stack.length - 1, 1);
modal_stack.modal("hide");
horizon.utils.loadAngular(container);
});

// After a modal has been fully hidden, remove it to avoid confusion.
Expand Down
35 changes: 24 additions & 11 deletions horizon/static/horizon/js/horizon.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ horizon.utils = {
},

/*
Adds commas to any integer or numbers within a string for human display.
Adds commas to any integer or numbers within a string for human display.
EG:
horizon.utils.humanizeNumbers(1234); -> "1,234"
horizon.utils.humanizeNumbers("My Total: 1234"); -> "My Total: 1,234"
*/
EG:
horizon.utils.humanizeNumbers(1234); -> "1,234"
horizon.utils.humanizeNumbers("My Total: 1234"); -> "My Total: 1,234"
*/
humanizeNumbers: function(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},

/*
Truncate a string at the desired length. Optionally append an ellipsis
to the end of the string.
Truncate a string at the desired length. Optionally append an ellipsis
to the end of the string.
EG:
horizon.utils.truncate("String that is too long.", 18, true); ->
"String that is too…"
*/
EG:
horizon.utils.truncate("String that is too long.", 18, true); ->
"String that is too…"
*/
truncate: function(string, size, includeEllipsis) {
if(string.length > size) {
if(includeEllipsis) {
Expand All @@ -40,5 +40,18 @@ horizon.utils = {
} else {
return string;
}
},

loadAngular: function (element) {
angular.injector(['ng', 'horizonApp']).
invoke(['$rootScope', '$compile', function ($rootScope, $compile) {
try {
$compile(element)($rootScope);
$rootScope.$apply();
} catch (err) {}
/*
Compilation fails when it could not find a directive, fails silently on this, it is an angular behaviour.
*/
}]);
}
};

0 comments on commit 4b528b3

Please sign in to comment.