有没有一种方法可以对提示的输入问题进行while循环,绑定它们并使所有答案排列在数组中?

克里斯蒂安·马修

我正在构建一个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));
    }
},

然后是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 }
            );

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

使用

会产生这个...

using foo1
using foo2

这有可能吗,我将如何去做。

安德鲁·科罗鲁克

这样的事情应该为您工作:

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));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档