Skip to content

Commit

Permalink
#106 - fixed recursiveness of _attached and _detached
Browse files Browse the repository at this point in the history
  • Loading branch information
kentmw committed Aug 9, 2015
1 parent 49f4e97 commit cdc7119
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
48 changes: 36 additions & 12 deletions modules/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
__isActive: false,
__isAttachedToParent: false,
__isDisposed: false,
__attachedCallbackInvoked: false,
__feedbackEvents: null,
/**
* Array of feedback when-then-to's. Example:
Expand Down Expand Up @@ -153,20 +154,14 @@
* @method detach
*/
detach: function() {
var wasAttachedToDOM = this.isAttached();
if (this.isAttachedToParent()) {
// Detach view from DOM
if (this.injectionSite) {
this.$el.replaceWith(this.injectionSite);
} else {
this.$el.detach();
}
if (wasAttachedToDOM) {
this._detached();
_.each(this.__childViews, function(view) {
view._detached();
});
}
this.invokeDetached();
this.undelegateEvents();
this.__isAttachedToParent = false;
}
Expand All @@ -182,11 +177,8 @@
this.render();
this.injectionSite = $el.replaceWith(this.$el);
this.delegateEvents();
if (this.isAttached()) {
this._attached();
_.each(this.__childViews, function(view) {
view._attached();
});
if (!this.__attachedCallbackInvoked && this.isAttached()) {
this.invokeAttached();
}
this.__isAttachedToParent = true;
}
Expand Down Expand Up @@ -477,6 +469,38 @@
}
},

/**
* Call this method when a view is attached to the DOM. It is recursive to child views, but checks whether each child view is attached.
* @method invokeAttached
*/
invokeAttached: function() {
// Need to check if each view is attached because there is no guarentee that if parent is attached, child is attached.
if (!this.__attachedCallbackInvoked) {
this._attached();
this.__attachedCallbackInvoked = true;
_.each(this.__childViews, function(view) {
if (view.isAttachedToParent()) {
view.invokeAttached();
}
});
}
},

/**
* Call this method when a view is detached from the DOM. It is recursive to child views.
* @method invokeDetached
*/
invokeDetached: function() {
// No need to check if child views are actually detached, because if parent is detached, children are detached.
if (this.__attachedCallbackInvoked) {
this._detached();
this.__attachedCallbackInvoked = false;
}
_.each(this.__childViews, function(view) {
view.invokeDetached();
});
},

/************** Private methods **************/

/**
Expand Down
48 changes: 36 additions & 12 deletions torso-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,7 @@
__isActive: false,
__isAttachedToParent: false,
__isDisposed: false,
__attachedCallbackInvoked: false,
__feedbackEvents: null,
/**
* Array of feedback when-then-to's. Example:
Expand Down Expand Up @@ -1498,20 +1499,14 @@
* @method detach
*/
detach: function() {
var wasAttachedToDOM = this.isAttached();
if (this.isAttachedToParent()) {
// Detach view from DOM
if (this.injectionSite) {
this.$el.replaceWith(this.injectionSite);
} else {
this.$el.detach();
}
if (wasAttachedToDOM) {
this._detached();
_.each(this.__childViews, function(view) {
view._detached();
});
}
this.invokeDetached();
this.undelegateEvents();
this.__isAttachedToParent = false;
}
Expand All @@ -1527,11 +1522,8 @@
this.render();
this.injectionSite = $el.replaceWith(this.$el);
this.delegateEvents();
if (this.isAttached()) {
this._attached();
_.each(this.__childViews, function(view) {
view._attached();
});
if (!this.__attachedCallbackInvoked && this.isAttached()) {
this.invokeAttached();
}
this.__isAttachedToParent = true;
}
Expand Down Expand Up @@ -1822,6 +1814,38 @@
}
},

/**
* Call this method when a view is attached to the DOM. It is recursive to child views, but checks whether each child view is attached.
* @method invokeAttached
*/
invokeAttached: function() {
// Need to check if each view is attached because there is no guarentee that if parent is attached, child is attached.
if (!this.__attachedCallbackInvoked) {
this._attached();
this.__attachedCallbackInvoked = true;
_.each(this.__childViews, function(view) {
if (view.isAttachedToParent()) {
view.invokeAttached();
}
});
}
},

/**
* Call this method when a view is detached from the DOM. It is recursive to child views.
* @method invokeDetached
*/
invokeDetached: function() {
// No need to check if child views are actually detached, because if parent is detached, children are detached.
if (this.__attachedCallbackInvoked) {
this._detached();
this.__attachedCallbackInvoked = false;
}
_.each(this.__childViews, function(view) {
view.invokeDetached();
});
},

/************** Private methods **************/

/**
Expand Down
Loading

0 comments on commit cdc7119

Please sign in to comment.