有没有办法为提示输入问题做一个while循环,绑定它们并在数组中提供所有答案?

Chr*_*hew 5 javascript arrays npm yeoman yeoman-generator

我正在构建一个Yeoman生成器,并且需要依赖项来自https://github.com/sboudrias/mem-fs-editor#copytplfrom-to-context-settingshttps://github.com/SBoudrias/Inquirer.js /

我的想法是能够向用户询问问题并重复相同的问题,即你想要添加另一个问题......如果用户添加了另一个问题,那么它将绑定并记录该答案,如果用户说"不"或命中返回提示将停止.

我想将所有答案绑定到一个可以传递给另一个对象函数的arrary,以便它可以将响应列为数组.

这是迄今为止的代码......首先是提示:

askForTest1: function () {
    if (this.type == 'foundation5') {
        var cb = this.async();

        var prompts = {
            type: 'input',
            name: 'test1',
            message: chalk.yellow('  What is your favorite movie'),
            default: 'Star Wars!'
        };

        this.prompt(prompts, function (props) {
            this.templatedata.test1 = props.test1;

            cb();
        }.bind(this));
    }
},
Run Code Online (Sandbox Code Playgroud)

然后是copyTpl对象,它将绑定模板构建的选项:这是我想要发生的所需输出...并记住这个副本tpl与提示一样存在于同一个index.js文件中.即这个模板......

       this.fs.copyTpl(
              this.templatePath('/index2.html'),
              this.destinationPath('app/index2.html'),
              { title: [this.templatedata.test1-a, this.templatedata.test1-b, this.templatedata.test1-c, ...], h1: this.applicationName }
            );
Run Code Online (Sandbox Code Playgroud)

结果......带有此代码的模板......

运用

会产生这个......

using foo1
using foo2
Run Code Online (Sandbox Code Playgroud)

这是可能的,我将如何做到这一点.

Sim*_*ias 5

这是一个相当简单的编程任务.

使用向用户提问的方法的递归.然后,如果用户回答"是添加更多",则只需再次调用相同的功能.

大致相似的东西:

initializing: function () {
  this.movies = [];
},

askMovie: function (cb) {
  cb = cb || this.async();

  var prompts = [{
      type: 'input',
      name: 'movie',
      message: chalk.yellow('  What is your favorite movie'),
      default: 'Star Wars!'
  }, {
    type: 'confirm',
    name: 'askAgain',
    message: 'ask again?'
  }];

  this.prompt(prompts, function (props) {
    this.movies.push(props.movie)
    if (props.askAgain) {
      this.askMovie(cb);
    } else {
      cb();
    }
  }.bind(this));
  }
}
Run Code Online (Sandbox Code Playgroud)


And*_*luk 4

像这样的东西应该适合你:

function() {
    var answers = {
        times: [],
        title: undefined,
        type: undefined
    };

    function promptMe(prompt, cb) {
        self.prompt(prompt, function (props) {
            if(props.answer!= "done") {
                answers.times.push(props.time);
                promptMe(prompt, cb);
            } else {
                cb();
            }
        });
    }

    var cb = this.async(),
        self = this,
        movieTypePrompt = {
            type: 'input',
            name: 'movieType',
            message: chalk.yellow('What is your favorite type of movie?'),
            default: 'Action'
        },
        movieTitilePrompt = {
            type: 'input',
            name: 'movieTitle',
            message: chalk.yellow('What is your favorite movie?'),
            default: 'Tron: Legacy'
        }
        movieTimePrompt = {
            type: 'input',
            name: 'time',
            message: chalk.yellow('When can you watch a movie? (enter \'done\' when you are done entering times)'),
            default: '8PM'
        };

    //Prompt for movie type and title
    this.prompt([movieTypePrompt, movieTitlePrompt], function (props) {
        answers.title = props.movieTitle;
        answers.type = props.movieType;

        //Repeatedly prompt for movie times
        promptMe(moviePrompt, function() {
            console.log('done');

            cb();
        });
    }.bind(this));
}
Run Code Online (Sandbox Code Playgroud)