├── .gitignore ├── .meteor ├── .finished-upgraders ├── .gitignore ├── .id ├── cordova-plugins ├── packages ├── platforms ├── release └── versions ├── README.md ├── canonically_diff_docs.sh ├── client ├── api-box.html ├── api-box.js ├── basic │ ├── api.md │ ├── basic-content.html │ ├── introduction.html │ ├── sections │ │ ├── accounts.md │ │ ├── collections.md │ │ ├── commandline.md │ │ ├── environment.md │ │ ├── file-structure.md │ │ ├── methods.md │ │ ├── mobile.md │ │ ├── packages.md │ │ ├── pubsub.md │ │ ├── session.md │ │ ├── templates.md │ │ └── tracker.md │ ├── toc.html │ ├── toc.js │ └── toc.less ├── common │ ├── principles.md │ └── quick-start.md ├── data.js ├── docs.less ├── full-api │ ├── api.md │ ├── api │ │ ├── accounts.md │ │ ├── assets.md │ │ ├── blaze.md │ │ ├── check.md │ │ ├── collections.md │ │ ├── connections.md │ │ ├── core.md │ │ ├── ejson.md │ │ ├── email.md │ │ ├── http.md │ │ ├── methods.md │ │ ├── mobile-config.md │ │ ├── packagejs.md │ │ ├── passwords.md │ │ ├── pubsub.md │ │ ├── reactive-var.md │ │ ├── session.md │ │ ├── templates.md │ │ ├── timers.md │ │ └── tracker.md │ ├── commandline.html │ ├── concepts.html │ ├── docs.html │ ├── docs.js │ ├── introduction.html │ ├── nameToId.js │ ├── packages.html │ ├── packages │ │ ├── accounts-ui.html │ │ ├── appcache.html │ │ ├── audit-argument-checks.html │ │ ├── coffeescript.html │ │ ├── jquery.html │ │ ├── less.html │ │ ├── markdown.html │ │ ├── oauth-encryption.html │ │ ├── random.html │ │ ├── underscore.html │ │ └── webapp.html │ ├── tableOfContents.html │ ├── tableOfContents.js │ └── toc.less ├── helpers.js ├── layout.html ├── layout.js ├── lib │ └── navigate.js ├── link-redirect.js ├── links.js ├── names.json ├── responsive.js ├── search.html ├── search.js └── splash.less ├── lib └── appcache-config.js ├── private └── canonicalize.js └── public ├── favicon.png ├── forkme_right_red_aa0000.png ├── logo.png └── robots.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /.meteor/.finished-upgraders: -------------------------------------------------------------------------------- 1 | # This file contains information which helps Meteor properly upgrade your 2 | # app when you run 'meteor update'. You should check it into version control 3 | # with your project. 4 | 5 | notices-for-0.9.0 6 | notices-for-0.9.1 7 | 0.9.4-platform-file 8 | notices-for-facebook-graph-api-2 9 | -------------------------------------------------------------------------------- /.meteor/.gitignore: -------------------------------------------------------------------------------- 1 | local 2 | -------------------------------------------------------------------------------- /.meteor/.id: -------------------------------------------------------------------------------- 1 | # This file contains a token that is unique to your project. 2 | # Check it into your repository along with the rest of this directory. 3 | # It can be used for purposes such as: 4 | # - ensuring you don't accidentally deploy one app on top of another 5 | # - providing package authors with aggregated statistics 6 | 7 | tqr1dmhdrd0990n71v 8 | -------------------------------------------------------------------------------- /.meteor/cordova-plugins: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.meteor/packages: -------------------------------------------------------------------------------- 1 | # Meteor packages used by this project, one per line. 2 | # 3 | # 'meteor add' and 'meteor remove' will edit this file for you, 4 | # but you can also edit it by hand. 5 | 6 | standard-app-packages 7 | jquery 8 | underscore 9 | showdown 10 | spiderable 11 | appcache 12 | reload-safetybelt 13 | simple:markdown-templating 14 | simple:highlight.js 15 | less 16 | -------------------------------------------------------------------------------- /.meteor/platforms: -------------------------------------------------------------------------------- 1 | server 2 | browser 3 | ios 4 | -------------------------------------------------------------------------------- /.meteor/release: -------------------------------------------------------------------------------- 1 | METEOR@1.1.0.2 2 | -------------------------------------------------------------------------------- /.meteor/versions: -------------------------------------------------------------------------------- 1 | appcache@1.0.4 2 | autoupdate@1.2.1 3 | base64@1.0.3 4 | binary-heap@1.0.3 5 | blaze@2.1.2 6 | blaze-tools@1.0.3 7 | boilerplate-generator@1.0.3 8 | callback-hook@1.0.3 9 | check@1.0.5 10 | ddp@1.1.0 11 | deps@1.0.7 12 | ejson@1.0.6 13 | fastclick@1.0.3 14 | geojson-utils@1.0.3 15 | html-tools@1.0.4 16 | htmljs@1.0.4 17 | http@1.1.0 18 | id-map@1.0.3 19 | jquery@1.11.3_2 20 | json@1.0.3 21 | launch-screen@1.0.2 22 | less@1.0.14 23 | livedata@1.0.13 24 | logging@1.0.7 25 | markdown@1.0.4 26 | meteor@1.1.6 27 | meteor-platform@1.2.2 28 | minifiers@1.1.5 29 | minimongo@1.0.8 30 | mobile-status-bar@1.0.3 31 | mongo@1.1.0 32 | observe-sequence@1.0.6 33 | ordered-dict@1.0.3 34 | random@1.0.3 35 | reactive-dict@1.1.0 36 | reactive-var@1.0.5 37 | reload@1.1.3 38 | reload-safetybelt@1.0.3 39 | retry@1.0.3 40 | routepolicy@1.0.5 41 | session@1.1.0 42 | showdown@1.0.4 43 | simple:highlight.js@1.0.9 44 | simple:markdown-templating@1.2.6 45 | spacebars@1.0.6 46 | spacebars-compiler@1.0.6 47 | spiderable@1.0.7 48 | standard-app-packages@1.0.5 49 | templating@1.1.1 50 | tracker@1.0.7 51 | ui@1.0.6 52 | underscore@1.0.3 53 | url@1.0.4 54 | webapp@1.2.0 55 | webapp-hashing@1.0.3 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # meteor-doc-cn 2 | Meteor中文文档 3 | 4 | 文档在线地址:[http://docs.meteorhub.org/](http://docs.meteorhub.org/) 5 | 6 | Meteor中文社区[MeteorHub](http://www.meteorhub.org/),新社区,建设中。感兴趣的童鞋可以一起参与进来。 7 | 8 | QQ群:327885034 9 | 10 | **如果想贡献翻译,先到 [MeteorHub](http://www.meteorhub.org/t/meteor-meteor/40) 认领待翻译的章节,然后按照贡献流程进行。** 11 | 12 | ## 翻译进度 13 | 14 | - basic 15 | - 快速开始 (完成) by [zicai] 16 | - Meteor理念 (完成) by [zicai] 17 | - 学习资源 (完成) by [zicai] 18 | - 命令行工具 (完成) by [zicai] 19 | - 文件结构 (完成) by [zicai] 20 | - 开发手机应用 (完成) by [zicai] 21 | - 模板 (完成) by [zicai] 22 | - Session (完成) by [zicai] 23 | - Tracker (完成) by [zicai] 24 | - 集合 (完成) by [zicai] 25 | - 账户 (完成) by [zicai] 26 | - Methods (完成) by [zicai] 27 | - 发布/订阅 (完成) by [fooying] 28 | - Environment (完成) by [zicai] 29 | - full api 30 | - Concepts 31 | - what is Meteor 32 | - structring your app 33 | - Data and security 34 | - Reactivity 35 | - Live HTMl templates 36 | - Using packages 37 | - Namespacing 38 | - Deploying 39 | - Writing packages 40 | - API 41 | - Core (进行中) by [Huibean] 42 | - Publish and subscribe 43 | - Methods 44 | - Check 45 | - Server connections 46 | - Collections 47 | - Session 48 | - Accounts 49 | - Passwords 50 | - Templates 51 | - Blaze 52 | - Timers 53 | - Tracker 54 | - ReactiveVar 55 | - EJSON 56 | - HTTP 57 | - Email 58 | - package.js 59 | - mobile-config.js 60 | - Packages 61 | - Command line 62 | 63 | 64 | ## 贡献力量 65 | 66 | 如果想做出贡献的话,你可以: 67 | 68 | - 帮忙校对,挑错别字、病句等等 69 | - 提出修改建议 70 | - 提出术语翻译建议 71 | 72 | ## 贡献流程 73 | 74 | ### 如果是挑错别字,病句 75 | 76 | 直接在仓库中找到对应的文件,在线编辑修改提交即可。 77 | 78 | ### 如果是提修改建议 79 | 80 | 直接提issue就OK 81 | 82 | ### 如果是贡献翻译 83 | 84 | 这里有一个简单的流程,供大家参考: 85 | 86 | 1. 首先fork MeteorHub 的meteor-doc-cn仓库 87 | 2. 把fork过去的项目也就是你的项目clone到你的本地 88 | 3. 在命令行运行 `git branch develop` 来创建一个新分支 89 | 4. 运行 `git checkout develop` 来切换到新分支 90 | 5. 运行 `git remote add upstream git@github.com:MeteorHub/meteor-doc-cn.git` 把MeteorHub的库添加为远端库 91 | 6. 运行 `git remote update`更新 92 | 7. 运行 `git fetch upstream master` 拉取meteor-doc-cn的更新到本地 93 | 8. 运行 `git rebase -i upstream/master` 将meteor-doc-cn的更新合并到你的分支 94 | 95 | 这是一个初始化流程,只需要做一遍就行,之后请一直在develop分支进行修改。 96 | 97 | 如果修改过程中meteor-doc-cn有了更新,请重复6、7、8步。 98 | 99 | 修改之后,首先push到你的库,然后登录GitHub,在你的库的首页可以看到一个 `pull request` 按钮,点击它,填写一些说明信息,然后提交即可。 100 | 101 | 102 | [zicai]:https://github.com/zicai 103 | [fooying]:https://github.com/fooying 104 | [Huibean]:https://github.com/Huibean -------------------------------------------------------------------------------- /canonically_diff_docs.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | echo "Please run this script from the docs app folder." 3 | echo "Make sure you have phantomjs installed!" 4 | 5 | DOCS_FOLDER=$(pwd); 6 | METEOR_FOLDER=$(dirname ${DOCS_FOLDER}); 7 | 8 | # make temporary folder 9 | mkdir /tmp/docsdiff 10 | cd /tmp/docsdiff 11 | 12 | # trigger phantomjs to give us actual HTML 13 | curl "localhost:3000/?_escaped_fragment_=key1=value1" > new 14 | curl "docs.meteor.com/?_escaped_fragment_=key1=value1" > old 15 | 16 | # use our handy canonicalize script copy-pasted from the test-helpers package 17 | # maybe there is a way to use the actual package? 18 | ${METEOR_FOLDER}/scripts/node.sh "${DOCS_FOLDER}/private/canonicalize.js" new > new1 19 | ${METEOR_FOLDER}/scripts/node.sh "${DOCS_FOLDER}/private/canonicalize.js" old > old1 20 | 21 | # remove some of the things we want to ignore, you might want to change these 22 | cat new1 | sed "s/new-api-box//g" | sed "s/ class=\"api-title\"//g" > new2 23 | cat new2 | sed "s/
<\/p>//g" > new3 24 | cat new3 | sed "s/i>/em>/g" > new4 25 | 26 | cat old1 | sed "s/new-api-box//g" | sed "s/ class=\"api-title\"//g" > old2 27 | cat old2 | sed "s/
<\/p>//g" > old3
28 | cat old3 | sed "s/i>/em>/g" > old4
29 |
30 | # git diff is more colorful than regular diff
31 | /usr/bin/git diff -U10 --no-index --ignore-blank-lines -w old4 new4
--------------------------------------------------------------------------------
/client/api-box.html:
--------------------------------------------------------------------------------
1 |
2 | {{dstache}}content}}
3 |
17 |
18 |
19 |
20 | {{#with apiData this}}
21 |
62 | {{/with}}
63 |
64 |
65 |
66 |
67 | {{#each this}}
68 |
79 |
80 |
--------------------------------------------------------------------------------
/client/api-box.js:
--------------------------------------------------------------------------------
1 | var apiData = function (options) {
2 | options = options || {};
3 | if (typeof options === "string") {
4 | options = {name: options};
5 | }
6 |
7 | var root = DocsData[options.name];
8 |
9 | if (! root) {
10 | console.log("API Data not found: " + options.name);
11 | }
12 |
13 | if (_.has(options, 'options')) {
14 | root = _.clone(root);
15 | var includedOptions = options.options.split(';');
16 | root.options = _.filter(root.options, function (option) {
17 | return _.contains(includedOptions, option.name);
18 | });
19 | }
20 |
21 | return root;
22 | };
23 |
24 | var typeLink = function (displayName, url) {
25 | return "" + displayName + "";
26 | };
27 |
28 | var toOrSentence = function (array) {
29 | if (array.length === 1) {
30 | return array[0];
31 | } else if (array.length === 2) {
32 | return array.join(" or ");
33 | }
34 |
35 | return _.initial(array).join(", ") + ", or " + _.last(array);
36 | };
37 |
38 | var typeNameTranslation = {
39 | "function": "Function",
40 | EJSON: typeLink("EJSON-able Object", "#ejson"),
41 | EJSONable: typeLink("EJSON-able Object", "#ejson"),
42 | "Tracker.Computation": typeLink("Tracker.Computation", "#tracker_computation"),
43 | MongoSelector: [
44 | typeLink("Mongo Selector", "#selectors"),
45 | typeLink("Object ID", "#mongo_object_id"),
46 | "String"
47 | ],
48 | MongoModifier: typeLink("Mongo Modifier", "#modifiers"),
49 | MongoSortSpecifier: typeLink("Mongo Sort Specifier", "#sortspecifiers"),
50 | MongoFieldSpecifier: typeLink("Mongo Field Specifier", "#fieldspecifiers"),
51 | JSONCompatible: "JSON-compatible Object",
52 | EventMap: typeLink("Event Map", "#eventmaps"),
53 | DOMNode: typeLink("DOM Node", "https://developer.mozilla.org/en-US/docs/Web/API/Node"),
54 | "Blaze.View": typeLink("Blaze.View", "#blaze_view"),
55 | Template: typeLink("Blaze.Template", "#blaze_template"),
56 | DOMElement: typeLink("DOM Element", "https://developer.mozilla.org/en-US/docs/Web/API/element"),
57 | MatchPattern: typeLink("Match Pattern", "#matchpatterns"),
58 | "DDP.Connection": typeLink("DDP Connection", "#ddp_connect")
59 | };
60 |
61 | Template.autoApiBox.helpers({
62 | apiData: apiData,
63 | typeNames: function typeNames (nameList) {
64 | // change names if necessary
65 | nameList = _.map(nameList, function (name) {
66 | // decode the "Array.The Meteor API
4 |
5 | Javascript代码可以运行在两种环境:客户端(浏览器),和服务端(服务器上的[Node.js](http://nodejs.org/)容器)。
6 | 在API 参考中,我们会指出每个函数是否可以在客户端,或是服务端,或是*Anywhere(客户端和服务端)*被调用。
7 |
8 | {{> basicTemplates}}
9 | {{> basicSession}}
10 | {{> basicTracker}}
11 | {{> basicCollections}}
12 | {{> basicAccounts}}
13 | {{> basicMethods}}
14 | {{> basicPubsub}}
15 | {{> basicEnvironment}}
16 |
17 | {{/template}}
--------------------------------------------------------------------------------
/client/basic/basic-content.html:
--------------------------------------------------------------------------------
1 |
2 | {{> basicIntroduction}}
3 | {{> basicApi}}
4 | {{> basicPackages}}
5 |
6 | {{#markdown}}
7 | # 查看完整API 文档
8 |
9 | 恭喜你,你已经看完了Meteor基础文档。更多高级的功能和详细的介绍,请看 [完整版 API
10 | 文档](#/full/).
11 | {{/markdown}}
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/client/basic/introduction.html:
--------------------------------------------------------------------------------
1 |
2 |
学习资源
24 |
25 |
26 |
27 |
28 | 有很多社区资源可以帮助你开发应用。如果你对Meteor感兴趣,希望你能参与其中!
29 |
30 |
31 |
51 | {{/markdown}}
52 | meteor
标签。
39 | Accounts
4 |
5 | 要增加账户功能,用`meteor add`添加下面的一个或多个包:
6 |
7 | - `accounts-ui`:这个包允许你通过在模板中使用`{{dstache}}> loginButtons}}`,来添加自动生成的登录UI,
8 | 用户可以登录。社区中有其它的替代选择,或者你也可以结合使用[advanced Accounts methods](#accounts)
9 | - `accounts-password`: 这个包允许用户通过密码登录。添加之后,`loginButtons`下拉框会自动增加邮箱和密码文本域。
10 | - `accounts-facebook`, `accounts-google`, `accounts-github`, `accounts-twitter`,
11 | 以及其它由社区贡献的第三方登录包,让你的用户可以通过第三方网站登录。
12 | 它们会自动添加登录按钮到`loginButtons`下拉框中。
13 |
14 |
15 | {{dstache}}> loginButtons}}
16 | Client
17 |
18 |
19 | 在HTML中引入`loginButtions`模板,就可以使用Meteor默认的登录UI。使用前,需要先添加`accounts-ui`包:
20 |
21 | ```
22 | $ meteor add accounts-ui
23 | ```
24 |
25 | {{> autoApiBox "Meteor.user"}}
26 |
27 | 从 [`Meteor.users`](#meteor_users) 集合中获取当前登录用户。等同于`Meteor.users.findOne(Meteor.userId())`。
28 |
29 | {{> autoApiBox "Meteor.userId"}}
30 |
31 | {{> autoApiBox "Meteor.users"}}
32 |
33 | 这个集合包含了所有注册用户,每个用户是一个文档。例如:
34 |
35 | ```
36 | {
37 | _id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f", // Meteor.userId()
38 | username: "cool_kid_13", // unique name
39 | emails: [
40 | // each email address can only belong to one user.
41 | { address: "cool@example.com", verified: true },
42 | { address: "another@different.com", verified: false }
43 | ],
44 | createdAt: Wed Aug 21 2013 15:16:52 GMT-0700 (PDT),
45 | profile: {
46 | // The profile is writable by the user by default.
47 | name: "Joe Schmoe"
48 | },
49 | services: {
50 | facebook: {
51 | id: "709050", // facebook id
52 | accessToken: "AAACCgdX7G2...AbV9AZDZD"
53 | },
54 | resume: {
55 | loginTokens: [
56 | { token: "97e8c205-c7e4-47c9-9bea-8e2ccc0694cd",
57 | when: 1349761684048 }
58 | ]
59 | }
60 | }
61 | }
62 | ```
63 |
64 | 一个用户文档可以包含任何你想保存的用户相关的数据。不过,Meteor会特殊对待下面的几个字段:
65 |
66 | - `username`: 一个唯一的字符串,可以标识用户。
67 | - `emails`: 一个对象的数组。对象包含属性
68 | `address` 和 `verified` 。一个邮箱地址只能属于一个用户。`verified`是一个布尔值,如果用户已经[验证
69 | 邮箱地址](#accounts_verifyemail)则为true。
70 | - `createdAt`: 用户文档创建时间。
71 | - `profile`: 一个对象,默认情况下用户可以用任何数据新建和更新该字段。
72 | - `services`: 包含第三方登录服务使用的数据的对象。例如,它的`reset`字段包含的token,用于
73 | [忘记密码](#accounts_forgotpassword)的超链接,它的`resume`字段包含的token,用于维持用户登录状态。
74 |
75 | 和所有的[Mongo.Collection](#collections)一样,在服务端,你可以获取用户集合
76 | 的所有文档,但是在客户端只能获取那些服务端发布的文档。
77 |
78 | 默认情况下,当前用户的`username`,`emails`,和`profile`会发布到客户端。
79 | 可以使用下面的代码发布当前用户的其它字段:
80 |
81 | // server
82 | Meteor.publish("userData", function () {
83 | if (this.userId) {
84 | return Meteor.users.find({_id: this.userId},
85 | {fields: {'other': 1, 'things': 1}});
86 | } else {
87 | this.ready();
88 | }
89 | });
90 |
91 | // client
92 | Meteor.subscribe("userData");
93 |
94 | 如果安装了autopublish包,那么所有用户的信息都会发布到所有客户端。包括`username`,
95 | `profile` ,以及`service`中所有可以公开的字段(例如:`services.facebook.id`,
96 | `services.twitter.screenName`)。另外,使用autopublish时,对于当前登录用户会发布更多的信息,
97 | 包括access token。这样就可以直接从客户端发起API调用。
98 |
99 |
100 | 默认情况下,用户可以通过[`Accounts.createUser`](#accounts_createuser)声明自己的`profile`字段,
101 | 也可以通过`Meteor.users.update`来修改它。要允许用户修改更多的字段,使用[`Meteor.users.allow`](#allow)
102 | ,要禁止用户对自己的文档做任何修改,使用:
103 |
104 | Meteor.users.deny({update: function () { return true; }});
105 |
106 |
107 | {{> autoApiBox "currentUser"}}
108 |
109 | {{/template}}
110 |
--------------------------------------------------------------------------------
/client/basic/sections/collections.md:
--------------------------------------------------------------------------------
1 | {{#template name="basicCollections"}}
2 |
3 | Collections
4 |
5 | Meteor用*集合*保存数据。集合里保存的Javascript对象叫做文档。使用`new Mongo.Collection`声明一个集合。
6 |
7 | {{> autoApiBox name="Mongo.Collection" options=""}}
8 |
9 | 调用`Mongo.Collection`构造函数创建一个集合对象,它的行为就像MongoDB 的集合。如果在创建集合时传入了一个name参数,那么声明的就是一个持久性集合 — 保存在服务端并可以发布到客户端。
10 |
11 | 要想使客户端代码和服务端代码都可以通过相同的API访问相同的集合,最好在一个客户端和服务端都能加载到的javascript文件中把集合声明为全局变量。
12 |
13 | 示例:声明两个命名的,持久性的集合作为全局变量:
14 |
15 | ```
16 | // In a JS file that's loaded on the client and the server
17 | Posts = new Mongo.Collection("posts");
18 | Comments = new Mongo.Collection("comments");
19 | ```
20 |
21 | 如果传入`null`作为name参数,那么创建出来的就是一个本地集合。本地集合不会在客户端和服务端之间进行同步;它只是javascript对象的临时集合,支持Mongo-style `find`, `insert`, `update`, 和 `remove`操作。
22 |
23 | 默认情况下,Meteor会自动发布所有集合里的文档到每一个连接上的客户端。要禁用此行为,必须移除`autopublish`包:
24 |
25 | ```
26 | $ meteor remove autopublish
27 | ```
28 |
29 | 然后,使用[`Meteor.publish`](#meteor_publish) 和
30 | [`Meteor.subscribe`](#meteor_subscribe)来指定集合的哪一部分发送到哪些客户端。
31 |
32 | 使用`findOne` 和 `find`从集合里检索文档。
33 |
34 | {{> autoApiBox name="Mongo.Collection#findOne" options="sort;skip;fields"}}
35 |
36 | `findOne`方法可以从集合里查找特定的文档。调用`findOne`时,通常都会传入一个特定文档的`_id`:
37 |
38 | ```
39 | var post = Posts.findOne(postId);
40 | ```
41 |
42 | 然而,也可以给`findOne`传入一个Mongo 选择器,Mongo选择器是一个对象,指明了目标文档要满足的一系列属性。
43 | 例如,下面的选择器
44 |
45 | ```
46 | var post = Posts.findOne({
47 | createdBy: "12345",
48 | title: {$regex: /first/}
49 | });
50 | ```
51 |
52 | 会匹配到下面的文档
53 |
54 | ```
55 | {
56 | createdBy: "12345",
57 | title: "My first post!",
58 | content: "Today was a good day."
59 | }
60 | ```
61 |
62 | 关于MongoDB query operators 例如`$regex`, `$lt` (小于),
63 | `$text` (文本搜索)的更多信息参见[MongoDB
64 | documentation](http://docs.mongodb.org/manual/reference/operator/query/)。
65 |
66 | 一个非常有用但是不那么明显的功能就是Mongo 选择器可以匹配数组里的元素。例如:下面的选择器
67 |
68 | ```
69 | Post.findOne({
70 | tags: "meteor"
71 | });
72 | ```
73 |
74 | 会匹配到下面的文档
75 |
76 | ```
77 | {
78 | title: "I love Meteor",
79 | createdBy: "242135223",
80 | tags: ["meteor", "javascript", "fun"]
81 | }
82 | ```
83 |
84 | `findOne`方法和[`Session.get`](#session_get)一样,也是响应式的,也就是说,如果你在[template helper](#template_helpers)或是 [`Tracker.autorun`](#tracker_autorun)回调函数中使用了`findOne`,如果`findOne`返回的文档发生了变化,那么模板就会自动重新渲染,计算会重新执行。
85 |
86 | 注意,如果`findOne`没有找到任何文档,则返回`null`,通常发生在文档还没加载或是已经从集合中移除,所以要有处理`null`值的准备。
87 |
88 |
89 | {{> autoApiBox name="Mongo.Collection#find" options="sort;skip;limit;fields"}}
90 |
91 | `find`方法和`findOne`类似,不同的是,它不返回单一文档,而是返回一个MongoDB *游标*。游标是一个特殊的对象,代表一个查询里会被返回的文档列表。可以在模板Helper里返回游标,或是其它可以返回数组的地方:
92 |
93 | ```
94 | Template.blog.helpers({
95 | posts: function () {
96 | // this helper returns a cursor of
97 | // all of the posts in the collection
98 | return Posts.find();
99 | }
100 | });
101 | ```
102 |
103 | ```
104 |
105 |
106 | {{dstache}}#each posts}}
107 | {{dstache}}title}}
108 | 命令行工具
4 |
5 | #### `meteor help`
6 |
7 | 获取 `meteor` 命令行使用帮助。运行 `meteor help` 会列出`meteor`所有命令。运行` meteor help
8 | Environment
4 |
5 | {{> autoApiBox "Meteor.isClient"}}
6 | {{> autoApiBox "Meteor.isServer"}}
7 |
8 | {{#note}}
9 | `Meteor.isServer`可以用来限制代码的运行位置,但是它不会阻止代码发送到客户端。任何你不想发送到客户端的敏感代码,例如包含密码或是认证机制的代码,都应该放到`server`文件夹。
10 | {{/note}}
11 |
12 | {{> autoApiBox "Meteor.startup"}}
13 |
14 | 在服务端,只要服务进程启动完成,回调函数就会执行。在客户端,只要页面ready,回调函数就会执行。
15 |
16 | 最佳实践是:把模板事件,模板Helper,`Meteor.methods`, `Meteor.publish`, 或是
17 | `Meteor.subscribe` 之外的代码包裹进 `Meteor.startup`,这样APP的代码就不会在环境准备好之前运行。
18 |
19 | 例如:当服务端启动时,如果数据库为空则创建一些初始数据,可以用下面的方式:
20 |
21 | ```
22 | if (Meteor.isServer) {
23 | Meteor.startup(function () {
24 | if (Rooms.find().count() === 0) {
25 | Rooms.insert({name: "Initial room"});
26 | }
27 | });
28 | }
29 | ```
30 |
31 | 如果在服务端进程启动完成之后,或是客户端,页面ready之后调用`Meteor.startup`,回调函数会立即执行。
32 |
33 | {{/template}}
34 |
--------------------------------------------------------------------------------
/client/basic/sections/file-structure.md:
--------------------------------------------------------------------------------
1 | {{#template name="basicFileStructure"}}
2 |
3 | ## [文件结构](#filestructure)
4 |
5 | 对于该如何组织应用的文件结构,Meteor是非常灵活的。它会自动加载所有文件,所以不需要再用`