就像在非官方的Meteor FAQ中所述,我认为它基本上解释了如何构建一个大型应用程序:
Where should I put my files?
The example apps in meteor are very simple, and don’t provide much insight. Here’s my current thinking on the best way to do it: (any suggestions/improvements are very welcome!)
lib/ # <- any common code for client/server. lib/environment.js # <- general configuration lib/methods.js # <- Meteor.method definitions lib/external # <- common code from someone else ## Note that js files in lib folders are loaded before other js files. collections/ # <- definitions of collections and methods on them (could be models/) client/lib # <- client specific libraries (also loaded first) client/lib/environment.js # <- configuration of any client side packages client/lib/helpers # <- any helpers (handlebars or otherwise) that are used often in view files client/application.js # <- subscriptions, basic Meteor.startup code. client/index.html # <- toplevel html client/index.js # <- and its JS client/views/<page>.html # <- the templates specific to a single page client/views/<page>.js # <- and the JS to hook it up client/views/<type>/ # <- if you find you have a lot of views of the same object type client/stylesheets/ # <- css / styl / less files server/publications.js # <- Meteor.publish definitions server/lib/environment.js # <- configuration of server side packages public/ # <- static files, such as images, that are served directly. tests/ # <- unit test files (won't be loaded on client or server)
For larger applications, discrete functionality can be broken up into sub-directories which are themselves organized using the same pattern. The idea here is that eventually module of functionality could be factored out into a separate smart package, and ideally, shared around.
feature-foo/ # <- all functionality related to feature 'foo' feature-foo/lib/ # <- common code feature-foo/models/ # <- model definitions feature-foo/client/ # <- files only sent to the client feature-foo/server/ # <- files only available on the server
了解更多:非官方 Meteor 常见问题解答
mobile-config.js
文件吗? - Dude我同意yagooar的观点,但是不是用:
client/application.js
而是用:
client/main.js
main.*文件会最后被加载。这将有助于确保您没有任何加载顺序问题。有关更多详细信息,请参见Meteor文档http://docs.meteor.com/#structuringyourapp。
Meteor的设计让你可以以几乎任何想要的方式构建你的应用程序。因此,如果你不喜欢你的结构,你可以将文件移动到一个新目录,甚至将一个文件拆分成多个部分,对于Meteor而言,这几乎都是一样的。只需注意客户端、服务器和公共目录的特殊处理,如主文档页面所指定: http://docs.meteor.com/。
将所有内容混在一个HTML文件中肯定不会成为最佳实践。
以下是一个可能的结构示例: 在我的一个应用程序中,一个讨论论坛中,我按模块或“页面类型”(主页、论坛、主题、评论)组织,将每个页面类型的 .css、.html 和 .js 文件放在一起的一个目录中。我还有一个“基础”模块,其中包含常见的.css和.js代码以及主模板,它使用{{renderPage}}来渲染路由器中的另一个模块。
my_app/
lib/
router.js
client/
base/
base.html
base.js
base.css
home/
home.html
home.js
home.css
forum/
forum.html
forum.js
forum.css
topic/
topic.html
topic.js
topic.css
comment/
comment.html
comment.js
comment.css
您还可以按功能进行组织
my_app/
lib/
router.js
templates/
base.html
home.html
forum.html
topic.html
comment.html
js/
base.js
home.js
forum.js
topic.js
comment.js
css/
base.css
home.css
forum.css
topic.css
comment.css
我希望会涌现出一些更具体的最佳实践结构和命名约定。
把所有东西都混在一起!根据文档:
> HTML files in a Meteor application are treated quite a bit differently
> from a server-side framework. Meteor scans all the HTML files in your
> directory for three top-level elements: <head>, <body>, and
> <template>. The head and body sections are seperately concatenated
> into a single head and body, which are transmitted to the client on
> initial page load.
>
> Template sections, on the other hand, are converted into JavaScript
> functions, available under the Template namespace. It's a really
> convenient way to ship HTML templates to the client. See the templates
> section for more.
对于所有在谷歌上搜索这个主题的人:
em
命令行工具(由EventedMind开发,是Iron Router背后的团队)在设置新的Meteor App时非常有帮助。它将创建一个漂亮的文件夹结构。如果你已经在一个应用程序上工作并且想要重新组织它,只需使用em
设置一个新项目,就可以用它作为灵感来源。
请参见: https://github.com/EventedMind/em
以及这里:https://stackoverflow.com/questions/17509551/what-is-the-best-way-to-organize-templates-in-meteor-js
我认为《探索 Meteor》这本书的文件结构非常好,是一个很好的起点。
/app:
/client
main.html
main.js
/server
/public
/lib
/collections
当然,不是所有的东西都适用于这种方法,但在大型应用程序中,您将拥有许多可被隔离出来并可重复使用的功能。任何可分离和可复用的东西都适合放在包中,其余的则按照其他回答中提到的常规目录结构进行处理。即使您不制作包以避免开销,以模块化方式组织代码也是一个好主意(请参见这些建议)。
Meteor允许对文件加载进行细粒度控制(加载顺序、位置:客户端/服务器/两者等)以及包导出的内容。
我尤其发现在相关文件之间共享逻辑非常方便。例如,您想要创建一些实用函数并在不同的文件中使用。您只需在全局范围内(不加var
)定义它,Meteor将把它包装在包的命名空间中,因此它不会污染全局命名空间。
这里是官方文档。
经过一段时间的meteorjs编码之后,我很高兴有些空闲时间来致力于构建一个相当复杂的在线游戏。应用程序结构是我的首要关注点之一,看起来有几位非常优秀的程序员倡导了仅使用包来构建应用程序的方法,这使您能够松散地耦合功能上不同的包。该方法还有其他优点,并且可以在这里找到两篇解释该方法的非常好的文章:
http://www.matb33.me/2013/09/05/meteor-project-structure.html http://www.manuel-schoebel.com/blog/meteorjs-package-only-app-structure-with-mediator-pattern
view/order/checkout/templates.html
的内容:<template name="orderCheckout"></template>
<template name="paymentPanel"></template>
<template name="orderCheckoutSummary"></template>
<template name="paypalReturnOrderCheckout"></template>
├── cart │ ├── addItem │ │ ├── autoform.js │ │ ├── events.js │ │ ├── helpers.js │ │ ├── onRendered.js │ │ ├── routers.js │ │ ├── styles.less │ │ └── templates.html │ ├── checkout │ │ ├── autoform.js │ │ ├── events.js │ │ ├── helpers.js │ │ ├── onRendered.js │ │ ├── routers.js │ │ └── templates.html │ └── view │ ├── autoform.js │ ├── deleteItem │ │ ├── events.js │ │ ├── helpers.js │ │ └── templates.html │ ├── editItem │ │ ├── autoform.js │ │ ├── events.js │ │ ├── helpers.js │ │ └── templates.html │ ├── events.js │ ├── helpers.js │ ├── onDestroyed.js │ ├── onRendered.js │ ├── routers.js │ ├── styles.less │ └── templates.html我们还使用WebStorm进行开发,这是一个非常强大和灵活的Meteor开发编辑器。我们发现在搜索和组织代码以及高效地工作时,它非常有帮助。
使用 iron-cli 脚手架命令行工具,可以让事情变得非常简单。
https://github.com/iron-meteor/iron-cli
安装完成后,使用 iron create my-app
命令创建一个新项目。它将为您创建以下结构。您也可以在现有项目中使用此命令,只需在项目目录中使用 iron migrate
命令即可。
my-app/
.iron/
config.json
bin/
build/
config/
development/
env.sh
settings.json
app/
client/
collections/
lib/
stylesheets/
templates/
head.html
lib/
collections/
controllers/
methods.js
routes.js
packages/
private/
public/
server/
collections/
controllers/
lib/
methods.js
publish.js
bootstrap.js