我是一名javascript初学者,通过编写您的第一个生成器教程并阅读其他生成器代码,我已经设法使用我的第一个生成器。
我已经尝试了各种方法来使这项工作失败,如果有人能指出我正确的方向,我将不胜感激。我应该指出我使用了发电机 - 发电机v0.4.3
哎呀忘了说问题是从味道列表中得到回应。askFor: function () {
var done = this.async();
var prompts = [{
name: 'sitename',
message: 'What would you like to name your app/site?',
default: 'New Project'
},{
type: 'checkbox',
name: 'features',
message: 'Please choose additional features',
choices:[{
name: 'Modernizr',
value: 'includeModernizr',
checked: false
},{
name: 'Respond',
value: 'includeRespond',
checked: false
}]
},{
type: 'list',
name: 'flavour',
message: 'Which type of css would you like to use?',
choices: [{
name: 'Sass',
value: 'includeSass'
},{
name: 'Sass with Compass',
value: 'includeCompass'
},{
name: 'Stylus',
value: 'includeStylus'
},{
name: 'Standard css',
value: 'includeCss'
}]
,default: 1
},{
when: function (props) {
return props.flavour.indexOf('includeSass') !== -1;
},
type: 'confirm',
name: 'libsass',
value: 'includeLibSass',
message: 'Would you like to use libsass? Read about libsass at \n' + chalk.green('https://github.com/andrew/node-sass#nodesass'),
default: false
}];
this.prompt(prompts, function (props) {
var features = props.features;
function hasFeature (feat) {
return features.indexOf(feat) !== -1;
}
this.sitename = props.sitename;
this.includeModernizr = hasFeature('includeModernizr');
this.includeRespond = hasFeature('includeRespond');
//this is obviously the wrong way to do it
this.includeSass = props.includeSass;
this.includeLibSass = props.libsass;
this.includeRubySass = !props.libsass;
done();
}.bind(this));
},
答案 0 :(得分:1)
您可以使用lodash中的_.includes,而不是使用hasFeature函数。这是一种简单的方法来提取选择。
this.includeModernizr = _.includes(props.features, 'includeModernizr');
答案 1 :(得分:-1)
我似乎已经为自己解决了这个问题。我添加了另一个函数hasFlavour与hasFeature相同,但是通常使用flavor并且它正在工作。如果这是错误的方式,请告诉我。