Skip to content

Commit

Permalink
Bug fix regarding loading directive classes
Browse files Browse the repository at this point in the history
Previously there would be an issue if a single element defined two
directives as inspection of further elements would occur after the
first directive was initialised.
  • Loading branch information
stuffaboutpete committed Dec 4, 2014
1 parent 8b1e631 commit ba0a0d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/Jagged/Helper/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ define(
return array.filter(function(element, position, self) {
return self.indexOf(element) == position;
});
},

'static public containsElement (mixed, array) -> boolean': function(element, array)
{
return (array.indexOf(element) > -1);
},

'static public removeElement (mixed, array) -> array': function(element, array)
{
var index = array.indexOf(element);
// @todo Throw if -1
array.splice(index, 1);
return array;
}

});
29 changes: 21 additions & 8 deletions src/Jagged/Initialiser.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
define(

'require Jagged.Helper.String',
'require Jagged.Helper.Array',
'require Jagged.Templater',
'require Jagged.Injector',
'require Jagged.IDirective',

'class Jagged.Initialiser',
{

'private injector (Jagged.Injector)': null,
'private namespaces ([string])': null,
'private allElements ([HTMLElement])': [],
'private directiveClassLoading (object)': null,
'private loadingPaused (boolean)': false,
'private directiveClassesLoading ([object])': [],
'private loadingPausedDirectives ([Jagged.IDirective])': [],

'public construct ([string], Jagged.Injector) -> undefined': function(customNamespaces, injector)
{
Expand Down Expand Up @@ -53,13 +55,13 @@ define(

'public pauseLoading (Jagged.IDirective) -> undefined': function(directive)
{
if (directive === this.directiveClassLoading().instance) this.loadingPaused(true);
this.loadingPausedDirectives('push', directive);
},

'public unPauseLoading (Jagged.IDirective) -> undefined': function(directive)
{
if (directive === this.directiveClassLoading().instance) {
this.loadingPaused(false);
if (Jagged.Helper.Array.containsElement(directive, this.loadingPausedDirectives())) {
Jagged.Helper.Array.removeElement(directive, this.loadingPausedDirectives());
this.inspectNextElement();
}
},
Expand Down Expand Up @@ -135,7 +137,8 @@ define(
// Record that we are loading the class so
// that we can tell later whether we have
// finished processing or not
this.directiveClassLoading({

this.directiveClassesLoading('push', {
className: className,
element: element,
tagValue: directivesFound[i].tagValue,
Expand All @@ -152,6 +155,14 @@ define(
'private runDirective (string) -> undefined': function(directiveClass)
{

var directiveClassesLoading = this.directiveClassesLoading();
for (var i = 0; i < directiveClassesLoading.length; i++) {
if (directiveClassesLoading[i].className == directiveClass) {
var directiveData = directiveClassesLoading.splice(i, 1)[0];
break;
}
}

var directive = this.injector().resolve(directiveClass);

// Ensure the instanciated class is an instance
Expand All @@ -163,7 +174,6 @@ define(
);
}

var directiveData = this.directiveClassLoading();
directiveData.instance = directive;

// Allow the directive to initialise by providing
Expand All @@ -172,7 +182,10 @@ define(
directive.initialise(directiveData.element, directiveData.tagValue);

// Move on to the next element in the queue
if (!this.loadingPaused()) this.inspectNextElement();
// if we aren't waiting for anything
if (!this.directiveClassesLoading().length && !this.loadingPausedDirectives().length) {
this.inspectNextElement();
}

},

Expand Down

0 comments on commit ba0a0d4

Please sign in to comment.