Skip to content

Commit 48502aa

Browse files
author
Ilya Radchenko
committed
Namespace default config/account controllers
1 parent 966ed09 commit 48502aa

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

README.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ This is a small Node.JS library for loading Strider extensions.
1414
## API
1515

1616
```
17-
var Loader = require('strider-extension-loader')
18-
, loader = new Loader()
17+
var Loader = require('strider-extension-loader');
18+
var loader = new Loader();
1919
```
2020

21-
### new Loader(lesspaths)
22-
`lesspaths` is an optional list of directories that will be made
23-
available while compiling plugins' `less` style files.
21+
### new Loader(lesspaths, isNamespaced)
22+
* `lesspaths` is an optional list of directories that will be made
23+
available while compiling plugins' `less` style files.
24+
* `isNamespaced` is for backwards compatibility with older versions
25+
where the default type controllers e.g. `JobController` were not namespaced.
26+
For versions < 1.6.0 this property should NOT be set.
2427

2528
### .collectExtensions(dirs, done(err))
2629

@@ -30,7 +33,7 @@ Collect all strider extensions found in the given directories.
3033

3134
Load the "webapp" portion of all extensions.
3235

33-
`extensions` looks like `{plugintype: {pluginid: loadedPlugin, ... }, ...}`
36+
`extensions` looks like `{ plugintype: { pluginid: loadedPlugin, ... }, ... }`
3437

3538
The structure of the `loadedPlugin` object depend on the plugin type.
3639
- job:
@@ -42,7 +45,7 @@ Same as `initWebAppExtensions` but for the `worker` portion.
4245
### .initTemplates(done(err, templates))
4346

4447
Load all of the templates from all extensions. `templates` looks like
45-
`{templatename: stringtemplate, ...}`.
48+
`{ templatename: stringtemplate, ... }`.
4649

4750
### .initStaticDirs(app, done(err))
4851

@@ -64,7 +67,7 @@ Then the js and css are written to the files specified `jspath` and
6467

6568
Html for the templates are available on the `configs` objects.
6669

67-
Configs look like `{plugintype: {pluginid: config, ...}, ...}` and
70+
Configs look like `{ plugintype: { pluginid: config, ...}, ... }` and
6871
`config` looks like:
6972

7073
```js
@@ -86,7 +89,7 @@ package.json.
8689
"title": "My Plugin",
8790
"icon": "icon.png", // should be in the ./static dir
8891
"config": {
89-
"controller": // defaults to "JobController" for job plugins, "ProviderController", etc.
92+
"controller": // defaults to "Config.JobController" for job plugins, "Config.ProviderController", etc.
9093
"script": // path where the js should be loaded from. Path defaults to "config/config.js"
9194
"style": // defaults to "config/config.less"
9295
"template": // defaults to "config/config.html"
@@ -100,7 +103,7 @@ I hope that's clear.
100103

101104
If you don't need to do anything fancy, you can just use the default
102105
controller for your plugin type. Take a look in
103-
[strider's public/javascript/pages/config.js](asd) for the source of
106+
[strider's `client/config/controllers` directory][config-controllers] for the source of
104107
those controllers. Basically, each controller makes available a
105108
`config` object on the scope, which is populated by the plugin's
106109
config for the currently selected branch. Also a `save()` function is
@@ -180,7 +183,7 @@ To declare your npm package as a strider plugin, include a
180183
"tplname": "path/to/tpl.html"
181184
},
182185
"config": { // project-specific configuration
183-
"controller": // defaults to "JobController" for job plugins, "ProviderController", etc.
186+
"controller": // defaults to "Config.JobController" for job plugins, "Config.ProviderController", etc.
184187
"script": // path where the js should be loaded from. Path defaults to "config/config.js"
185188
"style": // defaults to "config/config.less". Can be less or css
186189
"template": // defaults to "config/config.html"
@@ -508,28 +511,28 @@ module.exports = function (context, done) {
508511
This is what gets passed into the `basic` init function, as well as
509512
the `listen` and `routes` functions of various plugin types.
510513

511-
- config ; main strider config
512-
- emitter ; for passing events
514+
- config -- main strider config
515+
- emitter -- for passing events
513516
- models
514517
- logger
515518
- middleware
516519
- auth
517-
- app ; the express app
520+
- app -- the express app
518521
- registerBlock
519522

520523
#### registerBlock(name, cb)
521524

522525
```javascript
523-
ctx.registerBlock('HeaderBrand', function(context, cb){
526+
ctx.registerBlock('HeaderBrand', function(context, cb) {
524527
// context has a lot of useful stuff on it:
525528

526-
var email = context.currentUser.user.email
529+
var email = context.currentUser.user.email;
527530

528531
// You can do some async processing here, but bear in mind
529532
// you'll be blocking the page load.
530533

531-
cb(null, "<h1>FooStrider</h1>");
532-
})
534+
cb(null, '<h1>FooStrider</h1>');
535+
});
533536
```
534537

535538
#### Templates in strider.json
@@ -541,8 +544,8 @@ strider.json:
541544
```javascript
542545
{
543546
"templates": {
544-
"HeaderBrand" : "<h1>An HTML String</h1>",
545-
"FooterTOS" : "./path/to/TOS.html"
547+
"HeaderBrand": "<h1>An HTML String</h1>",
548+
"FooterTOS": "./path/to/TOS.html"
546549
}
547550
}
548551
```
@@ -555,3 +558,5 @@ If you want to simply 'append' to a block, use the `registerBlock` method
555558
and make sure that you prefix the html you return with:
556559
`ctx.content` which will contain either the default html, or the content from
557560
previous extensions.
561+
562+
[config-controllers]: https://github.com/Strider-CD/strider/tree/master/client/config/controllers

lib/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@ On a grand scale, the things that plugins should be able to do:
2323

2424
module.exports = Loader
2525

26-
function Loader(lesspaths) {
26+
function Loader(lesspaths, isNamespaced) {
27+
this.isNamespaced = isNamespaced;
2728
this.types = {
2829
'job': job,
2930
'runner': runner,
3031
'provider': provider,
3132
'basic': basic
32-
}
33-
this.ids = {}
34-
this.lesspaths = lesspaths || []
33+
};
34+
this.ids = {};
35+
this.lesspaths = lesspaths || [];
3536
this.extensions = {
3637
job: {},
3738
runner: {},
3839
provider: {},
3940
basic: {}
40-
}
41+
};
4142
}
4243

4344
Loader.prototype = {
@@ -146,6 +147,8 @@ Loader.prototype = {
146147
, configs = {}
147148
, self = this
148149
this.allExtensions(function (type, id, plugin, next) {
150+
var namespace = self.isNamespaced ? 'Config.' : '';
151+
149152
if (!plugin.config) {
150153
plugin.config = {}
151154
}
@@ -156,7 +159,7 @@ Loader.prototype = {
156159
}
157160
}
158161
plugin.config = _.extend({
159-
controller: type[0].toUpperCase() + type.slice(1) + 'Controller',
162+
controller: namespace + type[0].toUpperCase() + type.slice(1) + 'Controller',
160163
/* Defaults. If they don't exist, however, no error is thrown.
161164
template: 'config/config.html',
162165
script: 'config/config.js',
@@ -206,8 +209,10 @@ Loader.prototype = {
206209
, types = this.types
207210
, self = this
208211
this.allExtensions(function (type, id, plugin, next) {
209-
var name = types[type].userConfig || 'user'
210-
, config = name + 'Config'
212+
var namespace = self.isNamespaced ? 'Account.' : '';
213+
var name = types[type].userConfig || 'user';
214+
var config = name + 'Config';
215+
211216
if (!plugin[config]) return next();
212217
if (!configs[type]) configs[type] = {}
213218
if (plugin[config] === true) {
@@ -216,7 +221,7 @@ Loader.prototype = {
216221
}
217222
}
218223
plugin[config] = _.extend({
219-
controller: type[0].toUpperCase() + type.slice(1) + 'Controller',
224+
controller: namespace + type[0].toUpperCase() + type.slice(1) + 'Controller',
220225
/* Defaults. If they don't exist, however, no error is thrown.
221226
template: 'config/user.html',
222227
script: 'config/user.js',

0 commit comments

Comments
 (0)