网站的电子画册怎么做,wordpress更改链接后网站打不开,除了wordpress还有什么,创意营销策划案例在Meteor中#xff0c;你只能使用包内的模块。你不能直接将模块与流星应用一起使用。此软件包解决了该问题 文章目录 源码下载地址安装定义软件包使用软件包在 Meteor 方法中使用 npm 模块的示例应用程序接口异步实用程序Async.runSync#xff08;函数#xff09;Meteor.sy…在Meteor中你只能使用包内的模块。你不能直接将模块与流星应用一起使用。此软件包解决了该问题 文章目录 源码下载地址安装定义软件包使用软件包在 Meteor 方法中使用 npm 模块的示例应用程序接口异步实用程序Async.runSync函数Meteor.sync函数Async.wrap函数Async.wrap对象函数名称Async.wrap对象函数名称列表 源码下载地址
点击这里下载源码
安装
meteor add meteorhacks:npm然后启动您的应用并按照说明进行操作。
定义软件包
初始化 npm 支持后你的应用内将有一个称为文件名的文件名。在该文件中定义包如下所示。
{redis: 0.8.2,github: 0.1.8
}您必须为 npm 模块定义一个绝对版本号 如果需要从特定提交安装 npm 模块请使用以下语法
{googleapis: https://github.com/bradvogel/google-api-nodejs-client/archive/d945dabf416d58177b0c14da64e0d6038f0cc47b.tar.gz
}以上内容可以使用 github 版本生成。你要用的是版本而不是.commit hash.tar.gzarchive/version number.tar.gz
使用软件包
你可以使用 method 访问服务器端的 npm 模块并随心所欲地使用它。 大多数 npm 模块都提供带有回调或承诺的异步 API。所以你不能直接在Meteor上使用它们。正因为如此这个软件包附带了一组方便的异步实用程序让你的生活更轻松。
在 Meteor 方法中使用 npm 模块的示例
if (Meteor.isClient) {getGists function getGists(user, callback) {Meteor.call(getGists, user, callback);}
}if (Meteor.isServer) {Meteor.methods({getGists: function getGists(user) {var GithubApi Meteor.npmRequire(github);var github new GithubApi({version: 3.0.0});var gists Async.runSync(function(done) {github.gists.getFromUser({user: arunoda}, function(err, data) {done(null, data);});});return gists.result;}});
}应用程序接口 仅在服务器端可用 Meteor.npmRequirenpmModule名称 此方法加载您在文件中指定的 NPM 模块。
var Github Meteor.npmRequire(github);Meteor.requirenpmModule名称 同上。但已弃用。
异步实用程序 仅在服务器端可用 Async Utitlies 可以通过 meteorhacksasync 作为单独的软件包提供 Meteor API 是同步执行的。大多数 NodeJS 模块都是异步工作的。 因此我们需要一种方法来弥补差距。Async Utilities 来拯救你。
Async.runSync函数
Async.runSync()暂停执行直到调用 callback如下所示。done()
var response Async.runSync(function(done) {setTimeout(function() { done(null, 1001);}, 100);
});console.log(response.result); // 1001done()callback 需要 2 个参数。 和对象。您可以将它们作为 的返回值获取如上例中的响应所示。errorresultAsync.runSync() 返回值是一个对象它有 2 个字段。 和。error result
Meteor.sync函数
相同但已弃用。Async.runSync
Async.wrap函数
包装一个异步函数并允许它在 Meteor 中运行没有回调。 //declare a simple async function
function delayedMessage(delay, message, callback) {setTimeout(function() {callback(null, message);}, delay);
}//wrapping
var wrappedDelayedMessage Async.wrap(delayedMessge);//usage
Meteor.methods({delayedEcho: function(message) {var response wrappedDelayedMessage(500, message);return response;}
});如果回调有结果它将从包装的函数返回。如果出现错误则会抛出。 Async.wrap(function)与 非常相似。 Meteor._wrapAsync Async.wrap对象函数名称
与 非常相似 但此 API 可用于包装对象的实例方法。Async.wrap(function)
var github new GithubApi({version: 3.0.0
});//wrapping github.user.getFrom
var wrappedGetFrom Async.wrap(github.user, getFrom);Async.wrap对象函数名称列表
与 非常相似 但此 API 可用于包装对象的多个实例方法。Async.wrap(object, functionName)
var github new GithubApi({version: 3.0.0
});//wrapping github.user.getFrom and github.user.getEmails
var wrappedGithubUser Async.wrap(github.user, [getFrom, getEmails]);//usage
var profile wrappedGithubUser.getFrom(arunoda);
var emails wrappedGithubUser.getEmails();