在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ Meteor package.js
Meteor結構
Meteor部署
Meteor排序
Meteor事件
Meteor Blaze
Meteor第一個應用程序
Meteor發(fā)布和訂閱
Meteor環(huán)境安裝配置
Meteor package.js
Meteor在手機上運行
Meteor集合
Meteor模板
Meteor跟蹤器
Meteor發(fā)送郵件
Meteor計時器
Meteor ToDo App實例
Meteor軟件包管理
Meteor方法
Meteor表單
Meteor Assets資源
Meteor會話
Meteor EJSON
Meteor http
Meteor安全
Meteor核心API
Meteor check
Meteor帳號
Meteor教程

Meteor package.js

在本章中,我們將學習如何創(chuàng)建自己的 meteor 包。

創(chuàng)建包

讓我們添加在桌面上的新文件夾用來創(chuàng)建新的包。使用命令提示符窗口執(zhí)行如下命令。
C:\Users\Administrator\Desktop\meteorApp> mkdir packages 

現(xiàn)在我們可以在上面創(chuàng)建的文件夾中創(chuàng)建你的包。運行命令提示符運行以下命令。Username 是您的 Meteor 開發(fā)者的用戶名和package-name是你的包的名稱。

C:\Users\Administrator\Desktop\meteorApp\packages>meteor create --package username:package-name

添加包

為了能夠本地包添加到我們的應用程序,需要設置環(huán)境變量(ENVIRONMENT VARIABLE),將告訴 meteor 從本地文件夾加載包。右鍵單擊"計算機"圖標,然后選擇屬性/高級系統(tǒng)設置/環(huán)境變量/新建。

變量名應該是PACKAGE_DIR變量值路徑應該是指向創(chuàng)建的文件夾。在我們的示例中是:C:\Users\Administrator\Desktop\meteorApp\packages.

注意:不要忘記在添加新的環(huán)境變量后重新啟動命令提示符。
現(xiàn)在,我們可以運行下面的代碼將包添加到我們的應用程序-
C:\Users\Administrator\Desktop\meteorApp>meteor add username:package-name

包文件

如果打開創(chuàng)建軟件包的文件夾,你會看到四個文件。
  • package-name-test.js
  • package-name.js
  • package.js
  • README.md

測試包(package-name-test.js)

Meteor 提供 tinytest 包用于測試。讓我們先從命令提示符窗口來安裝它。
C:\Users\Adminitrator\Desktop\meteorApp>meteor add tinytest

如果你打開 package-name-test.js, 你會看到默認的測試例子。我們將用這個例子來測試應用程序。開發(fā) Meteor 包時,你應該要寫寫自己的測試。

要測試包,需要運行在命令提示符下此代碼。
 C:\Users\Administrator\Desktop>meteor test-packages packages/package-name
你應該得到以下結果。

Meteor Package Test

package.js文件

我們可以在這個文件中編寫代碼?,F(xiàn)在我們在包中實現(xiàn)一些簡單的功能。包將日志記錄一些文本到控制臺。

packages/package.js

myPackageFunction = function() {
   console.log('This is simple package...');
}

package-name.js文件

這是我們可以設定一些程序包配置文件。 我們一會再回到它,但現(xiàn)在需要導出myPackageFunction,就可以在應用程序中使用它了。我們需要添加這些到 Package.onUse 函數(shù)內。該文件將是這個樣子。

packages/package-name.js

Package.describe({
   name: 'username:package-name',
   version: '0.0.1',
   // Brief, one-line summary of the package.
   summary: '',
   // URL to the Git repository containing the source code for this package.
   git: '',
   // By default, Meteor will default to using README.md for documentation.
   // To avoid submitting documentation, set this field to null.
   documentation: 'README.md'
});

Package.onUse(function(api) {
   api.versionsFrom('1.2.1');
   api.use('ecmascript');
   api.addFiles('mypackage.js');
   api.export('myPackageFunction'); // We are exporting the function we created above...
});

Package.onTest(function(api) {
   api.use('ecmascript');
   api.use('tinytest');
   api.use('username:package-name');
   api.addFiles('package-name-tests.js');
});

使用軟件包

現(xiàn)在,我們終于可以從 app.js 文件調用myPackageFunction()函數(shù)了。

packages/package.js

if(Meteor.isClient) {
   myPackageFunction();
}
控制臺將日志記錄包中的文本,其結果如下:
Meteor Package Log
為了更好地理解package.js文件如何配置,我們將使用Meteor 官方文檔的例子。

這是一個例子文件...看看一就知道了

/* Information about this package */
Package.describe({
   // Short two-sentence summary.
   summary: "What this does",
   // Version number.
   version: "1.0.0",
   // Optional.  Default is package directory name.
   name: "username:package-name",
   // Optional github URL to your source repository.
   git: "https://github.com/something/something.git",
});

/* This defines your actual package */
Package.onUse(function (api) {
   // If no version is specified for an 'api.use' dependency, use the
   // one defined in Meteor 0.9.0.
   api.versionsFrom('0.9.0');
   // Use Underscore package, but only on the server.
   // Version not specified, so it will be as of Meteor 0.9.0.
   api.use('underscore', 'server');
   // Use iron:router package, version 1.0.0 or newer.
   api.use('iron:router@1.0.0');
   // Give users of this package access to the Templating package.
   api.imply('templating')
   // Export the object 'Email' to packages or apps that use this package.
   api.export('Email', 'server');
   // Specify the source code for the package.
   api.addFiles('email.js', 'server');
});

/* This defines the tests for the package */
Package.onTest(function (api) {
   // Sets up a dependency on this package
   api.use('username:package-name');
   // Allows you to use the 'tinytest' framework
   api.use('tinytest@1.0.0');
   // Specify the source code for the package tests
   api.addFiles('email_tests.js', 'server');
});

/* This lets you use npm packages in your package*/
Npm.depends({
   simplesmtp: "0.3.10",
   "stream-buffers": "0.2.5"
});

上一篇:Meteor EJSON下一篇:Meteor集合