Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two Fixes: Components Style, LiveScript #505

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
[![browser support](https://ci.testling.com/derbyjs/derby.png)](https://ci.testling.com/derbyjs/derby)
[![build status](https://api.travis-ci.org/derbyjs/derby.png)](http://travis-ci.org/derbyjs/derby)

# Changes in this fork

- support for local component styles
- support for subcomponents
- support for components written in LiveScript


# Derby

The Derby MVC framework makes it easy to write realtime, collaborative applications that run in both Node.js and browsers.
Expand Down
38 changes: 36 additions & 2 deletions lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,53 @@ SingletonComponentFactory.prototype.init = function(context) {
// Don't call the create method for singleton components
SingletonComponentFactory.prototype.create = function() {};

App.prototype.component = function(viewName, constructor) {
if (typeof viewName === 'function') {
App.prototype.component = function(viewName, constructor, ns) {
if (viewName && (typeof viewName === 'function' || typeof viewName === 'object')) {
constructor = viewName;
viewName = null;
}

constructor = extractLiveScriptConstructor(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 {
throw new Error('No view name specified for component');
}
}

if (constructor.prototype.style) {
var styleFilename = constructor.prototype.style;
this.loadStyles(styleFilename);
}

// Load sub components
var subComponents = constructor.prototype.components;
if (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
var view = this.views.find(viewName);
if (!view) {
Expand All @@ -197,6 +219,18 @@ App.prototype.component = function(viewName, constructor) {
return this;
};


function extractLiveScriptConstructor(constructor) {
if (typeof constructor === 'object') {
var keys = Object.keys(constructor);
if (keys.length == 1) {
return constructor[keys[0]];
}
}
return constructor;
}


function extendComponent(constructor) {
// Don't do anything if the constructor already extends Component
if (constructor.prototype instanceof Component) return;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
},
"dependencies": {
"chokidar": "^1.2.0",
"derby-parsing": "^0.4.1",
"derby-templates": "^0.3.3",
"derby-parsing": "michael-brade/derby-parsing",
"derby-templates": "michael-brade/derby-templates",
"html-util": "^0.2.1",
"racer": "^0.7.0",
"racer": "michael-brade/racer",
"resolve": "^1.1.6",
"through": "^2.3.4",
"tracks": "^0.5.0"
Expand Down