0

I'm using node-typekit to create a new Typekit empty font set in my Yeoman generator for use in my project. I am able to successfully create the kit, but cannot figure out how to return the kit id value back to the Yeoman generator so I can add the necessary Typekit script tag values to my web pages. Here is the part of my index.js generator script in question:

At the top of index.js:

var kit = require('typekit');
var typekitID = '';

function setTypekitID(theid) {
    typekitID = theid;
};

And the app section:

app: function () {

  var token = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  var split = this.domainname.split('.');
  split.pop();
  var localdomain = split.join('.') + '.dev';

  kit.create(token, {
    name: this.appname,
    badge: false,
    domains: [this.domainname, localdomain],
    families: []
  }, function (err, data) {
    setTypekitID(data.kit.id);
  });

}

If instead of:

setTypekitID(data.kit.id);

I use:

console.log(data.kit.id);

The correct kit ID is displayed in the console. However, I can't quite figure out how to pass the data.kit.id value in the callback back to the generator for further use. Given the current code above, it comes back as "undefined".

Any ideas? Thanks!

4

1 回答 1

0

没有任何经验typekit,我猜这kit.create是一个异步调用。当你进行这样的调用时,生成器需要知道它,因此它可以延迟调用进一步的方法,直到它知道你的异步回调有机会执行。

尝试这样做:

app: function () {
  var done = this.async(); // this tells the generator, "hang on, yo."
  // ...
  kit.create(token, { /* ... */ }, function (err, data) {
    setTypekitID(data.kit.id);

    done(); // calling this resumes the generator.
  });
}

generator-generator可以在此处找到默认如何使用它的示例。

于 2014-03-14T18:50:02.030 回答