Skip to content

Commit

Permalink
refactor: collapse child registration and child unregistration into a…
Browse files Browse the repository at this point in the history
… single invocation in the post-link of directives.
  • Loading branch information
stristr committed Jun 28, 2014
1 parent b9451f0 commit a450ffb
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/scripts/directives/fa-container-surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
};
}
Expand Down
4 changes: 1 addition & 3 deletions src/scripts/directives/fa-flipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ angular.module('famous.angular')
isolate.renderNode.flip(overrideOptions || scope.$eval(attrs.faOptions));
};

$famousDecorator.unregisterChild(element, scope);

$famousDecorator.sequenceWith(
scope,
function(data) {
Expand All @@ -85,7 +83,7 @@ angular.module('famous.angular')
transclude(scope, function (clone) {
element.find('div').append(clone);
});
$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/directives/fa-grid-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/directives/fa-header-footer-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/directives/fa-image-surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@example
* To use `fa-image-surface`, declare an `fa-image-url` attribute with a string url.
* ```html
* <fa-image-surface
* <fa-image-surface
* fa-image-url="img/my-image.png"
* class="img"
* fa-color="'blue'"
Expand All @@ -25,7 +25,7 @@
* ```
* `Fa-image-surface` accepts two css-style properties: `color` and `background color`, which may be assigned values by the `fa-color` and `fa-background-color` attributes respectively.
*
* `Fa-size` may also be declared as an attribute. If void, the `fa-image-surface` will inherit the size of its parent node.
* `Fa-size` may also be declared as an attribute. If void, the `fa-image-surface` will inherit the size of its parent node.
*/

angular.module('famous.angular')
Expand Down Expand Up @@ -99,7 +99,7 @@ angular.module('famous.angular')

attrs.$observe('faImageUrl', updateContent);

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/scripts/directives/fa-modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,6 @@ angular.module('famous.angular')

isolate.renderNode = new RenderNode().add(isolate.modifier)

// When the actual element is destroyed by Angular,
// "hide" the Modifier by setting its opacity to 0.
$famousDecorator.unregisterChild(element, scope, function() {
isolate.modifier.setOpacity(0);
});


$famousDecorator.sequenceWith(scope, function(data) {
isolate.renderNode.add(data.renderNode);
});
Expand All @@ -378,7 +371,11 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate, function() {
// When the actual element is destroyed by Angular,
// "hide" the Modifier by setting its opacity to 0.
isolate.modifier.setOpacity(0);
});

// Trigger a $digest loop to make sure that callbacks for the
// $observe listeners are executed in the compilation phase.
Expand Down
4 changes: 1 addition & 3 deletions src/scripts/directives/fa-render-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ angular.module('famous.angular')

isolate.renderNode = scope.$eval(attrs.faNode);

$famousDecorator.unregisterChild(element, scope);

$famousDecorator.sequenceWith(scope, function(data) {
isolate.renderNode.add(data.renderNode);
isolate.children.push(data);
Expand All @@ -94,7 +92,7 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/directives/fa-scrollview.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);

}
};
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/directives/fa-sequential-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
};
}
Expand Down
7 changes: 3 additions & 4 deletions src/scripts/directives/fa-surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ angular.module('famous.angular')
if (attrs.class) {
isolate.renderNode.setClasses(attrs['class'].split(' '));
}

// TODO: hook into RenderController and hide this render node
$famousDecorator.unregisterChild(element, scope);
},
post: function(scope, element, attrs){
var isolate = $famousDecorator.ensureIsolate(scope);
Expand All @@ -217,7 +214,9 @@ angular.module('famous.angular')
angular.element(element[0].querySelectorAll('div.fa-surface')).append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate, function() {
// TODO: hook into RenderController and hide this render node
});
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/scripts/directives/fa-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ angular.module('famous.angular')
size: scope.$eval(attrs.faSize) || [undefined, undefined]
});

$famousDecorator.unregisterChild(element, scope);

$famousDecorator.sequenceWith(scope, function(data) {
isolate.renderNode.add(data.renderNode);
isolate.children.push(data);
Expand All @@ -83,7 +81,7 @@ angular.module('famous.angular')
element.find('div').append(clone);
});

$famousDecorator.registerChild(scope, isolate);
$famousDecorator.registerChild(scope, element, isolate);
}
}
}
Expand Down
33 changes: 8 additions & 25 deletions src/scripts/services/famousDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* ```js
* var isolate = $famousDecorator.ensureIsolate($scope);
*
* $famousDecorator.registerChild($scope, isolate);
*
* $famousDecorator.unregisterChild($element, $scope);
* $famousDecorator.registerChild($element, $scope, isolate);
*
* $famousDecorator.sequenceWith(
* $scope,
Expand Down Expand Up @@ -83,42 +81,27 @@ angular.module('famous.angular')
* @module famous.angular
* @description
* Register a child isolate's renderNode to the nearest parent that can sequence
* it.
* it, and set up an event listener to remove it when the associated element is destroyed
* by Angular.
*
* A `registerChild` event is sent upward with `scope.$emit`.
*
* @param {String} scope - the scope with an isolate to be sequenced.
* @param {String} scope - the scope with an isolate to be sequenced
* @param {String} element - the element to listen for destruction on
* @param {Object} isolate - an isolated scope object from $famousDecorator#ensureIsolate
* @param {Function} unregisterCallback - an optional callback to invoke when unregistration is complete
* @returns {void}
*/
registerChild: function(scope, isolate) {
registerChild: function(scope, element, isolate, unregisterCallback) {
scope.$emit('registerChild', isolate);
},

/**
* @ngdoc method
* @name $famousDecorator#unregisterChild
* @module famous.angular
* @description
* An `unregisterChild` event is sent upward with `scope.$emit` when a a directive element's
* `$destroy` event occurs.
*
* @param {Object} element - the element to listen on
* @param {String} scope - the scope to emit from
* @param {Object} callback - an optional callback to invoke when the emission is complete
* @returns {void}
*/
unregisterChild: function(element, scope, callback) {
var ensureIsolate = this.ensureIsolate;
element.one('$destroy', function() {
var isolate = ensureIsolate(scope);

if ('removeMethod' in isolate) {
isolate.removeMethod(isolate.id);
}

// Invoke the callback, if provided
callback && callback();
unregisterCallback && unregisterCallback();
});
},

Expand Down

0 comments on commit a450ffb

Please sign in to comment.