Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/angularAMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

});
98 changes: 94 additions & 4 deletions test/unit/app_no_ngload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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('<span ng-bind="1+2"></span>')($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('<span amd-require="test/testModule">Content {{1+2}}</span>')($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('<span amd-base-url="test" amd-require="./testModule">Content {{1+2}}</span>')($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('<span amd-base-url="test/dir" amd-require="./testModule1 ../testModule">Content {{1+2}}</span>')($rootScope);

$rootScope.$digest();
expect(element.html()).not.toContain('Content');

waits(100);

runs(function() {
$rootScope.$digest();
expect(element.html()).toContain('3');
})
});
});
});
});

});
3 changes: 2 additions & 1 deletion test/unit/lib/main.no_ngload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down