Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
general cleanup, new way of loading templates in the generator intern…
Browse files Browse the repository at this point in the history
…ally, and added ability to pass model path to collection
  • Loading branch information
Matt Przybylski committed Oct 31, 2013
1 parent 560704b commit 3354029
Show file tree
Hide file tree
Showing 40 changed files with 196 additions and 208 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ yo bbr:router routername directory/to/put/it
yo bbr:view viewname directory/to/put/it
```

Collection subgenerators also take a third parameter (optional) that allows you to define the path to your model:

```
yo bbr:collection collectionname directory/to/put/it path/to/model
```

So, in the case that you have the following structure:

```
|-- js
| |-- models
| |-- test
| |-- test.js
```

And you want to create a collection for TestModel (the name of the model from test.js), you would run this:

```
yo bbr:collection test test test # first is name of collection, second is directory to create it in, third is path to model
```

This would generate the following:

```
|-- js
| |-- collections
| |-- test
| |-- test.js # TestCollection
| |-- models
| |-- test
| |-- test.js # TestModel
```

Now `TestCollection` references `TestModel` with the define statement looking for `models/test/test` and passing it in as `TestModel` for reference within the collection.

**NOTE: Do not put beginning or trailing slashes on the directory structure!**


Expand Down
50 changes: 22 additions & 28 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var Generator = module.exports = function Generator(args, options, config) {
}
});

this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'index.html'));

this.sourceRoot(path.join(__dirname, '../templates'));
this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'common/index.html'));
this.on('end', function () {
if (['app', 'bbr'].indexOf(this.generatorName) >= 0) {
this.installDependencies({ skipInstall: this.options['skip-install'] });
Expand All @@ -50,36 +50,34 @@ Generator.prototype.askFor = function askFor() {

function hasFeature(feat) { return features.indexOf(feat) !== -1; }

// this.includeRequireJS = true;

cb();
}.bind(this));
};

Generator.prototype.git = function git() {
this.copy('gitignore', '.gitignore');
this.copy('gitattributes', '.gitattributes');
this.copy('common/gitignore', '.gitignore');
this.copy('common/gitattributes', '.gitattributes');
};

Generator.prototype.bower = function bower() {
this.copy('bowerrc', '.bowerrc');
this.copy('_bower.json', 'bower.json');
this.copy('common/bowerrc', '.bowerrc');
this.copy('common/_bower.json', 'bower.json');
};

Generator.prototype.gruntfile = function gruntfile() {
this.template('Gruntfile.js');
this.template('common/Gruntfile.js', 'Gruntfile.js');
};

Generator.prototype.packageJSON = function packageJSON() {
this.template('_package.json', 'package.json');
this.template('common/_package.json', 'package.json');
};

Generator.prototype.configRB = function packageJSON() {
this.template('config.rb', 'config.rb');
this.template('common/config.rb', 'config.rb');
};

Generator.prototype.writeIndexWithRequirejs = function writeIndexWithRequirejs() {
this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'index.html'));
this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'common/index.html'));
this.indexFile = this.engine(this.indexFile, this);

this.indexFile = this.appendScripts(this.indexFile, 'js/main.js', [
Expand All @@ -97,27 +95,23 @@ Generator.prototype.setupEnv = function setupEnv() {
this.mkdir('app/sass');
this.mkdir('app/images');
this.mkdir('app/jade');
this.template('app/404.html');
this.template('app/favicon.ico');
this.template('app/robots.txt');
this.copy('app/htaccess', 'app/.htaccess');
this.template('common/app/404.html', 'app/404.html');
this.template('common/app/favicon.ico', 'app/favicon.ico');
this.template('common/app/robots.txt', 'app/robots.txt');
this.copy('common/app/htaccess', 'app/.htaccess');
this.write('app/index.html', this.indexFile);
};

Generator.prototype.mainStylesheet = function mainStylesheet() {
this.directory('mixins', 'app/sass/mixins');
this.template('_normalize.scss', 'app/sass/_normalize.scss');
this.template('_base.scss', 'app/sass/_base.scss');
this.template('main.scss', 'app/sass/main.scss');
this.directory('sass/mixins', 'app/sass/mixins');
this.template('sass/_normalize.scss', 'app/sass/_normalize.scss');
this.template('sass/_base.scss', 'app/sass/_base.scss');
this.template('sass/main.scss', 'app/sass/main.scss');
};

Generator.prototype.mainJs = function mainJs() {
this.sourceRoot(path.join(__dirname, '../templates'));

var mainJsFile = this.engine(this.read('main.js'), this);

this.write('app/js/main.js', mainJsFile);
this.template('../templates/app.js', 'app/js/app.js');
this.template('../templates/router.js', 'app/js/router/router.js');
this.template('../templates/abstract.js', 'app/js/views/abstract.js');
this.template('js/main.js', 'app/js/main.js');
this.template('js/app.js', 'app/js/app.js');
this.template('js/router-default.js', 'app/js/router/router.js');
this.template('js/abstract.js', 'app/js/views/abstract.js');
};
48 changes: 15 additions & 33 deletions collection/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
/*jshint latedef:false */
var path = require('path');
var util = require('util');
var yeoman = require('yeoman-generator');
var scriptBase = require('../script-base');
'use strict';

module.exports = Generator;
var path = require('path'),
util = require('util'),
yeoman = require('yeoman-generator'),
scriptBase = require('../script-base');

function Generator() {

var Generator = module.exports = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
scriptBase.apply(this, arguments);
this.sourceRoot(path.join(__dirname, '../templates'));
};

// required for collection.js template which uses `appname`
}

util.inherits(Generator, scriptBase);

Generator.prototype.createControllerFiles = function createControllerFiles() {
var ext = '.js',
directory = (typeof this.dirPath !== 'undefined') ? '/' + this.dirPath : '';
util.inherits(Generator, yeoman.generators.NamedBase, scriptBase);

var destFile = path.join('app/js/collections' + directory + '/', this.name + ext);
var template = [
'define([',
' \'underscore\',',
' \'backbone\',',
' \'models/' + this.name + '\'',
'], function(_, Backbone, ' + this._.classify(this.name) + 'Model' + ') {',
' \'use strict\';',
'',
' var ' + this._.classify(this.name) + 'Collection = Backbone.Collection.extend({',
' ' + 'model: ' + this._.classify(this.name) + 'Model',
' });',
'',
' return ' + this._.classify(this.name) + 'Collection;',
'});'
].join('\n');
Generator.prototype.createViewFiles = function createViewFiles() {
this.dirPath = (typeof this.arguments[1] !== 'undefined') ? '/' + this.arguments[1] : '';
this.pathToModel = (typeof this.arguments[2] !== 'undefined') ? this.arguments[2] + '/' : '';

this.write(destFile, template);
};
this.copy('js/collection.js', 'app/js/collections' + this.dirPath + '/' + this.name + '.js');
};
File renamed without changes.
62 changes: 15 additions & 47 deletions model/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,21 @@
/*jshint latedef:false */
var path = require('path');
var util = require('util');
var yeoman = require('yeoman-generator');
var scriptBase = require('../script-base');
'use strict';

module.exports = Generator;
var path = require('path'),
util = require('util'),
yeoman = require('yeoman-generator'),
scriptBase = require('../script-base');

function Generator() {
scriptBase.apply(this, arguments);

// XXX default and banner to be implemented
this.argument('attributes', {
type: Array,
defaults: [],
banner: 'field[:type] field[:type]'
});

// parse back the attributes provided, build an array of attr
this.attrs = this.attributes.map(function (attr) {
var parts = attr.split(':');
return {
name: parts[0],
type: parts[1] || 'string'
};
});

}

util.inherits(Generator, scriptBase);
var Generator = module.exports = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
scriptBase.apply(this, arguments);
this.sourceRoot(path.join(__dirname, '../templates'));
};

Generator.prototype.createModelFiles = function createModelFiles() {
var ext = '.js',
directory = (typeof this.dirPath !== 'undefined') ? '/' + this.dirPath : '';
util.inherits(Generator, yeoman.generators.NamedBase, scriptBase);

var destFile = path.join('app/js/models' + directory + '/', this.name + ext);
var template = [
'define([',
' \'underscore\',',
' \'backbone\'',
'], function(_, Backbone) {',
' \'use strict\';',
'',
' var ' + this._.classify(this.name) + 'Model = Backbone.Model.extend({',
' defaults: {}',
' });',
'',
' return ' + this._.classify(this.name) + 'Model;',
'});'
].join('\n');
Generator.prototype.createViewFiles = function createViewFiles() {
this.dirPath = (typeof this.arguments[1] !== 'undefined') ? '/' + this.arguments[1] : '';

this.write(destFile, template);
};
this.copy('js/model.js', 'app/js/models' + this.dirPath + '/' + this.name + '.js');
};
46 changes: 13 additions & 33 deletions router/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
/*jshint latedef:false */
'use strict';

var path = require('path'),
util = require('util'),
yeoman = require('yeoman-generator'),
scriptBase = require('../script-base');
util = require('util'),
yeoman = require('yeoman-generator'),
scriptBase = require('../script-base');

module.exports = Generator;

function Generator() {
var Generator = module.exports = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
scriptBase.apply(this, arguments);
this.sourceRoot(path.join(__dirname, '../templates'));
};

// required for router.js template which uses `appname`
}

util.inherits(Generator, scriptBase);

Generator.prototype.createControllerFiles = function createControllerFiles() {
var ext = '.js',
directory = (typeof this.dirPath !== 'undefined') ? '/' + this.dirPath : '';

var destFile = path.join('app/js/router' + directory + '/', this.name + ext);
var template = [
'define([',
' \'jquery\',',
' \'backbone\'',
'], function($, Backbone) {',
' \'use strict\';',
'',
' var ' + this._.classify(this.name) + 'Router = Backbone.Router.extend({',
' routes: {',
' }',
' });',
'',
' return ' + this._.classify(this.name) + 'Router;',
'});'
].join('\n');
util.inherits(Generator, yeoman.generators.NamedBase, scriptBase);

this.write(destFile, template);
Generator.prototype.createViewFiles = function createViewFiles() {
this.dirPath = (typeof this.arguments[1] !== 'undefined') ? '/' + this.arguments[1] : '';

};
this.copy('js/router-new.js', 'app/js/router' + this.dirPath + '/' + this.name + '.js');
};
2 changes: 0 additions & 2 deletions script-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ var Generator = module.exports = function Generator() {
var sourceRoot = '/templates/';
this.scriptSuffix = '.js';

this.dirPath = arguments[0][1];

this.sourceRoot(path.join(__dirname, sourceRoot));
};

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion app/templates/_bower.json → templates/common/_bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"requirejs": "~2.1.9",
"requirejs-text": "~2.0.10",
"modernizr": "~2.6.2",
"tweenMax": "~1.11.0"
"greensock": "~1.11.1"
},
"devDependencies": {}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions templates/js/collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
define([
'underscore',
'backbone',
'models/<%= pathToModel %><%= name %>'
], function(_, Backbone, <%= _.classify(name) %>Model) {
'use strict';

var <%= _.classify(name) %>Collection = Backbone.Collection.extend({
model: <%= _.classify(name) %>Model
});

return <%= _.classify(name) %>Collection;
});
2 changes: 1 addition & 1 deletion templates/main.js → templates/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require.config({
jquery: '../js/libs/bower/jquery/jquery',
backbone: '../js/libs/bower/backbone/backbone',
underscore: '../js/libs/bower/underscore/underscore',
tweenmax: '../js/libs/bower/tweenMax/src/minified/TweenMax.min'
tweenmax: '../js/libs/bower/greensock/src/minified/TweenMax.min'
}
});

Expand Down
12 changes: 12 additions & 0 deletions templates/js/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define([
'underscore',
'backbone'
], function(_, Backbone) {
'use strict';

var <%= _.classify(name) %>Model = Backbone.Model.extend({
defaults: {}
});

return <%= _.classify(name) %>Model;
});
File renamed without changes.
13 changes: 13 additions & 0 deletions templates/js/router-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
define([
'jquery',
'backbone'
], function($, Backbone) {
'use strict';

var <%= _.classify(name) %>Router = Backbone.Router.extend({
routes: {
}
});

return <%= _.classify(name) %>Router;
});
1 change: 1 addition & 0 deletions templates/js/view.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>View <%= _.classify(name) %>View created.</p>
Loading

0 comments on commit 3354029

Please sign in to comment.