ui-include.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. (function ($) {
  2. "use strict";
  3. var promise = false,
  4. deferred = $.Deferred();
  5. _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  6. $.fn.uiInclude = function(){
  7. if(!promise){
  8. promise = deferred.promise();
  9. }
  10. //console.log('start: includes');
  11. compile(this);
  12. function compile(node){
  13. node.find('[ui-include]').each(function(){
  14. var that = $(this),
  15. url = that.attr('ui-include');
  16. promise = promise.then(
  17. function(){
  18. //console.log('start: compile '+ url);
  19. var request = $.ajax({
  20. url: eval(url),
  21. method: "GET",
  22. dataType: "text"
  23. });
  24. //console.log('start: loading '+ url);
  25. var chained = request.then(
  26. function(text){
  27. //console.log('done: loading '+ url);
  28. var compiled = _.template(text.toString());
  29. var html = compiled({app: app});
  30. var ui = that.replaceWithPush( html );
  31. ui.find('[ui-jp]').uiJp();
  32. ui.find('[ui-include]').length && compile(ui);
  33. }
  34. );
  35. return chained;
  36. }
  37. );
  38. });
  39. }
  40. deferred.resolve();
  41. return promise;
  42. }
  43. $.fn.replaceWithPush = function(o) {
  44. var $o = $(o);
  45. this.replaceWith($o);
  46. return $o;
  47. }
  48. })(jQuery);