Skip to content

Commit

Permalink
allow to use subcomponents
Browse files Browse the repository at this point in the history
usage:
   MyComponent.prototype.name = 'maincomponent'
   MyComponent.prototype.components = [require('subcomponent')]

result:
   maincomponent
   maincomponent:subcomponent
  • Loading branch information
michael-brade committed Sep 2, 2015
1 parent 002bd27 commit 65858f3
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ SingletonComponentFactory.prototype.init = function(context) {
// Don't call the create method for singleton components
SingletonComponentFactory.prototype.create = function() {};

App.prototype.component = function(viewName, constructor) {
App.prototype.component = function(viewName, constructor, ns) {
if (typeof viewName === 'function') {
constructor = viewName;
viewName = null;
Expand All @@ -175,15 +175,23 @@ App.prototype.component = function(viewName, constructor) {
// Inherit from Component
extendComponent(constructor);

// Set namespace to the component if it's passed along
if (ns)
constructor.prototype.ns = ns;

// Load template view from filename
if (constructor.prototype.view) {
var viewFilename = constructor.prototype.view;
viewName = constructor.prototype.name || path.basename(viewFilename, '.html');
if (ns)
viewName = ns + ':' + viewName;
this.loadViews(viewFilename, viewName);

} else if (!viewName) {
if (constructor.prototype.name) {
viewName = constructor.prototype.name;
if (ns)
viewName = ns + ':' + viewName;
var view = this.views.register(viewName);
view.template = templates.emptyTemplate;
} else {
Expand All @@ -194,6 +202,15 @@ App.prototype.component = function(viewName, constructor) {
if (constructor.prototype.style) {
var styleFilename = constructor.prototype.style;
this.loadStyles(styleFilename);
}

// Load sub components
var subComponents = constructor.prototype.components;
if (subComponents) {
console.log("loading components: ", subComponents);
for(var i = 0, len = subComponents.length; i < len; i++) {
this.component(null, subComponents[i], viewName);
}
}

// Associate the appropriate view with the component type
Expand Down

0 comments on commit 65858f3

Please sign in to comment.