Skip to content

Commit 96ccfbe

Browse files
author
Alexander Castillo
committed
directive preload fix
This fixes #1
1 parent 1a436bb commit 96ccfbe

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

angular-css.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* CSS on-demand for AngularJS
3-
* @version v1.0.4
3+
* @version v1.0.5
44
* @author DOOR3, Alex Castillo
55
* @link http://door3.github.io/angular-css
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -299,8 +299,7 @@
299299
if (!stylesheets) {
300300
stylesheets = [];
301301
// Add all stylesheets from custom directives to array
302-
// @TODO: preload directives once we can parse css on directive register
303-
//if ($directives.length) Array.prototype.push.apply(stylesheets, $directives);
302+
if ($directives.length) Array.prototype.push.apply(stylesheets, $directives);
304303
// Add all stylesheets from ngRoute to array
305304
if ($injector.has('$route')) Array.prototype.push.apply(stylesheets, $css.getFromRoutes($injector.get('$route').routes));
306305
// Add all stylesheets from UI Router to array
@@ -438,22 +437,27 @@
438437
module.directive = function(directiveName, directiveFactory) {
439438
var originalDirectiveFactory = angular.isFunction(directiveFactory) ?
440439
directiveFactory : directiveFactory[directiveFactory.length - 1];
441-
var directive = {};
442-
directive.directiveName = directiveName;
443-
$directives.push(directive);
440+
try {
441+
var directive = angular.copy(originalDirectiveFactory)();
442+
directive.directiveName = directiveName;
443+
if (directive.hasOwnProperty('css')) $directives.push(directive);
444+
} catch (e) { }
444445
return originalDirective.apply(this, arguments);
445446
};
446447
module.config(['$provide','$injector', function ($provide, $injector) {
447-
angular.forEach($directives, function (directive) {
448-
var dirProvider = directive.directiveName + 'Directive';
448+
angular.forEach($directives, function ($directive) {
449+
var dirProvider = $directive.directiveName + 'Directive';
449450
if ($injector.has(dirProvider)) {
450-
$provide.decorator(dirProvider, ['$delegate', '$rootScope', function ($delegate, $rootScope) {
451+
$provide.decorator(dirProvider, ['$delegate', '$rootScope', '$timeout', function ($delegate, $rootScope, $timeout) {
451452
var directive = $delegate[0];
452453
var compile = directive.compile;
453-
directive.compile = function(element, attrs) {
454+
if (directive.css) $directive.css = directive.css;
455+
directive.compile = function(tElement, tAttrs) {
454456
var link = compile ? compile.apply(this, arguments): false;
455457
return function(scope, element, attrs) {
456-
if (link) link.apply(this, arguments);
458+
$timeout(function () {
459+
if (link) link.apply(this, arguments);
460+
});
457461
$rootScope.$broadcast('$directiveAdd', directive, scope);
458462
};
459463
};

angular-css.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-css",
33
"main": "./angular-css.js",
4-
"version": "1.0.4",
4+
"version": "1.0.5",
55
"homepage": "http://door3.github.io/angular-css",
66
"authors": [
77
"Alex Castillo <[email protected]>"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "angular-css",
33
"filename": "angular-css.js",
44
"description": "CSS on-demand for AngularJS",
5-
"version": "1.0.4",
5+
"version": "1.0.5",
66
"homepage": "http://door3.github.io/angular-css",
77
"repository": {
88
"type": "git",

0 commit comments

Comments
 (0)