首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Inquirer.js:允许用户编写列表响应

Inquirer.js:允许用户编写列表响应
EN

Stack Overflow用户
提问于 2015-03-25 21:05:45
回答 1查看 931关注 0票数 1

好吧,我被困在创造一个尤曼发电机了。我有一个提示符,它显示了三个不同选项的列表。看起来是这样的:

JS档案名称:-一-二-其他

我希望第三个选项允许用户编写自己的。我可以调用另一个提示方法吗?

代码语言:javascript
复制
// ****************************************************************************
// Author: Daniel Jenkins
// Date: 03/35/3015
// Purpose: A generator for creating js files including, name, date, and purpose fields.
// ****************************************************************************

var generators = require('yeoman-generator');

module.exports = generators.Base.extend({
  prompting: function() {
    var done = this.async();
    var myChoices = [this.appname, 'one', 'two', 'other'];
    var prompts = {
      type: 'list',
      name: 'fileName',
      message: 'Name of new JS file: ',
      choices: myChoices
    };

    // Select the filename from list.
    this.prompt(prompts, function(answers) {

      // Store user input as an argument for the EJS preprossor.
      this.context = {
        fileName: answers.fileName,
      };

      done();
    }.bind(this));
  },

  // Add file after filling in EJS template 
  copyMainFiles: function() {

    // Create a time object for todays date.
    var my_date = new Date();

    // Add date property. 
    this.context.fileDate = my_date.toDateString();

    // Run through EJS and create the file.
    this.template("_index.js", this.context.fileName + ".js", this.context);
  }
});

EN

回答 1

Stack Overflow用户

发布于 2015-04-30 15:03:01

您可以通过运行条件语句来使用when,以指定在何时调用另一个字段。如果语句返回true,则该字段将被调用,否则它将被跳过。

参见添加到代码中的内容:

代码语言:javascript
复制
module.exports = generators.Base.extend( {
    prompting : function () {
        var done = this.async();
        var myChoices = [ this.appname, 'one', 'two', 'other' ];
        var prompts = [ {
            type    : 'list',
            name    : 'fileName',
            message : 'Name of new JS file: ',
            choices : myChoices
        },{
            when    : function ( answers ) {
                return answers.fileName === 'other';
            },
            type    : 'input',
            name    : 'fileName',
            message : 'custom file name'
        }];

        // Select the filename from list.
        this.prompt( prompts, function ( answers ) {

            // Store user input as an argument for the EJS preprossor.
            this.context = {
                fileName : answers.fileName,
            };

            done();
        }.bind( this ) );
    },

    // Add file after filling in EJS template 
    copyMainFiles : function () {

        // Create a time object for todays date.
        var my_date = new Date();

        // Add date property. 
        this.context.fileDate = my_date.toDateString();

        // Run through EJS and create the file.
        this.template( "_index.js", this.context.fileName + ".js", this.context );
    }
} );
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29266499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档