From ae1615d3d775a1b6526bd992efd47ccadb15aa37 Mon Sep 17 00:00:00 2001 From: alancnet Date: Sun, 8 Jun 2014 12:17:17 -0700 Subject: [PATCH] added amd-require directive --- src/angularAMD.js | 64 ++++++++++++++++++++- test/unit/app_no_ngload.spec.js | 98 +++++++++++++++++++++++++++++++-- test/unit/lib/main.no_ngload.js | 3 +- 3 files changed, 159 insertions(+), 6 deletions(-) diff --git a/src/angularAMD.js b/src/angularAMD.js index 5b71e70..717fe1e 100644 --- a/src/angularAMD.js +++ b/src/angularAMD.js @@ -315,7 +315,69 @@ define(function () { } }; + + var mod = angular.module('amd', []); + mod.directive('amdRequire', [ + + // Angular services + '$compile', + + // Factory + function($compile) { + + return { + restrict: 'A', + priority: 1000, + scope: true, + replace: false, + transclude: true, + compile: function() { + return { + pre: function($scope, $element, attrs, ctrl, transclude) { + if (typeof attrs.amdBaseUrl !== 'undefined') { + if (attrs.amdBaseUrl.length && attrs.amdBaseUrl.charAt(attrs.amdBaseUrl.length - 1) != '/') { + // Add / to the end + attrs.amdBaseUrl += '/'; + } + $scope.requireBaseUrl = attrs.amdBaseUrl; + } + var deps = attrs.amdRequire + .split(' ') + // Reduce to non-empty strings, append baseUrl + .reduce(function(pv, cv, i, a) { + var item = cv.trim(); + if (item) { + var path; + if (item.charAt(0) == '.') { + // Apply relative pathing + while (item.indexOf('./') == 0) { + // Remove leading ./ + item = item.substr(2); + } + path = ($scope.requireBaseUrl || '') + item; + } else { + path = item; + } + pv.push(path); + } + return pv; + }, []); + // Load dependencies + require(deps, function() { + // Compile transclude + $scope.$apply(function() { + transclude($scope, function (clone) { + $element.append(clone); + }) + }) + }); + } // pre + } //return + } // compile + } // return + } // function + ]); // Create a new instance and return return new angularAMD(); - + }); diff --git a/test/unit/app_no_ngload.spec.js b/test/unit/app_no_ngload.spec.js index ace5566..3dc7823 100644 --- a/test/unit/app_no_ngload.spec.js +++ b/test/unit/app_no_ngload.spec.js @@ -10,7 +10,7 @@ define(['app_no_ngload','angularAMD'], function (app, angularAMD) { 'use strict'; describe('angularAMD_no_ngload', function () { //console.log("Running app_no_ngload.spec.js"); - + it('is created.', function () { expect(angularAMD).toBeDefined(); }); @@ -29,7 +29,97 @@ define(['app_no_ngload','angularAMD'], function (app, angularAMD) { it('.processQueue should throw an error.', function () { expect(angularAMD.processQueue).toThrow(); }); - + + }); + describe('angularAMD Module', function() { + var $compile, $rootScope; + beforeEach(function() { + angular.injector(['ng', 'amd']).invoke(['$compile', '$rootScope', function(_$compile, _$rootScope) { + $compile = _$compile; + $rootScope = _$rootScope; + }]) + }); + + it('Tests should load $compile', function() { + expect($compile).toBeDefined(); + expect($rootScope).toBeDefined(); + }); + + it('angular is present', function() { + expect(angular).toBeDefined(); + expect(angular.module).toBeDefined(); + }); + it('registers as an Angular module.', function() { + // expect(angular.module('amd')).toBeDefined(); + }); + + it('basic angular test', function() { + var element = $compile('')($rootScope); + $rootScope.$digest(); + expect(element.html()).toEqual('3'); + }); + + it('basic async test', function() { + var foo = 0; + runs(function() { + setTimeout(function() { + foo++; + }, 10); + }); + waits(20); + + runs(function() { + expect(foo).toEqual(1); + }) + }); + + describe('requireDirective', function() { + define('test/testModule', [], {}); + define('test/dir/testModule1', [], {}); + + it('compiles as an attribute', function() { + var element = $compile('Content {{1+2}}')($rootScope); + + $rootScope.$digest(); + expect(element.html()).not.toContain('Content'); + + waits(100); + + runs(function() { + $rootScope.$digest(); + expect(element.html()).toContain('3'); + }) + + }); + + it('can resolve dependencies relative to amd-base-url', function() { + var element = $compile('Content {{1+2}}')($rootScope); + + $rootScope.$digest(); + expect(element.html()).not.toContain('Content'); + + waits(100); + + runs(function() { + $rootScope.$digest(); + expect(element.html()).toContain('3'); + }) + + }); + + it('can traverse up tree from amd-base-url', function() { + var element = $compile('Content {{1+2}}')($rootScope); + + $rootScope.$digest(); + expect(element.html()).not.toContain('Content'); + + waits(100); + + runs(function() { + $rootScope.$digest(); + expect(element.html()).toContain('3'); + }) + }); + }); }); -}); - \ No newline at end of file +}); \ No newline at end of file diff --git a/test/unit/lib/main.no_ngload.js b/test/unit/lib/main.no_ngload.js index e63ee11..f612b28 100644 --- a/test/unit/lib/main.no_ngload.js +++ b/test/unit/lib/main.no_ngload.js @@ -14,7 +14,8 @@ require.config({ }, shim: { - 'angularAMD': ['angular'] + 'angularAMD': ['angular'], + 'angular': { exports: 'angular' } }, // controllerSpec is at the end of dependency tree so kicking it off will start entire test