Advertisement
Guest User

Untitled

a guest
May 15th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Behavior:
  3. When I am on mysite.com/path/anything/else, path.html is included (mysite.com/foo includes foo.html)
  4. Before including the appropriate html file, load a js file by the same name (/path so path.js). This file contains the controller. Now load html file and give it the appropriate controller (/path so pathCtrl)
  5. */
  6.  
  7. //in the main view - USAGE
  8. <vc-include src="contentFile"></vc-include>
  9.  
  10. //in my app config
  11. myApp.$controllerProvider = $controllerProvider; //cache this so I can register my controller
  12.  
  13. //in my main controller - determine the file (route)
  14.     $scope.$on('$locationChangeStart', function(event, target) {
  15.       var base = document.getElementsByTagName('base')[0].href;
  16.       var page =  target.replace(base, '').split('/')[0];
  17.       if (!page) { page = 'home'; }
  18.       $scope.contentFile = 'partials/'+page+'.html';
  19.     });
  20.  
  21. //in the dynamically loaded controller js file
  22. myApp.$controllerProvider.register('myCtrl', function($scope) { });
  23.  
  24. /* Here's my version of ng-include that makes this work. I had to replace $http with jQuery and therefore removed the $templateCache stuff. Other than updating the name to vc-include in a few place, my changes start at "var getInclude"   */
  25.  
  26. myApp.directive('vcInclude', ['$http', '$templateCache', '$anchorScroll', '$compile', '$animator',
  27.       function($http, $templateCache, $anchorScroll, $compile, $animator) {
  28.         return {
  29.           restrict: 'ECA',
  30.           terminal: true,
  31.           compile: function(element, attr) {
  32.             var srcExp = attr.vcInclude || attr.src,
  33.                 onloadExp = attr.onload || '',
  34.                 autoScrollExp = attr.autoscroll;
  35.  
  36.             return function(scope, element, attr) {
  37.               var animate = $animator(scope, attr);
  38.               var changeCounter = 0,
  39.                   childScope;
  40.  
  41.               var clearContent = function() {
  42.                 if (childScope) {
  43.                   childScope.$destroy();
  44.                   childScope = null;
  45.                 }
  46.                 animate.leave(element.contents(), element);
  47.               };
  48.  
  49.               scope.$watch(srcExp, function vcIncludeWatchAction(src) {
  50.                 var thisChangeId = ++changeCounter;
  51.  
  52.                 var getInclude = function() { //CALLBACK AFTER GETTING CONTROLLER!
  53.                   $.ajax({
  54.                     url: src,
  55.                     success: function(response) {
  56.                       if (thisChangeId !== changeCounter) { return; }
  57.                       if (childScope) { childScope.$destroy(); }
  58.                       childScope = scope.$new();
  59.                       animate.leave(element.contents(), element);
  60.                       var controller = src.replace('partials/', '');
  61.                       controller = controller.replace('.html', 'Ctrl');
  62.                       var contents = $('<div ng-controller"'+controller+'">').html(response).contents();
  63.                       animate.enter(contents, element);
  64.                       $compile(contents)(childScope);
  65.                       childScope.$emit('$includeContentLoaded');
  66.                       scope.$eval(onloadExp);
  67.                     },
  68.                     error: function() {
  69.                       if (thisChangeId === changeCounter) clearContent();
  70.                     }
  71.                   });
  72.                 };
  73.  
  74.                 if (src) { //GET CONTROLLER
  75.                   var controllerSrc = src.replace('.html', '.js');
  76.                   $.ajax({
  77.                     url: controllerSrc,
  78.                     success: getInclude,
  79.                     error: getInclude
  80.                   })
  81.                 }
  82.                 else { clearContent(); }
  83.               });
  84.             };
  85.           }
  86.         };
  87.       }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement