English 中文(简体)
如何更改 Meteor 装入 Javascript 文件的顺序?
原标题:How do I change the order in which Meteor loads Javascript files?

当您用 Meteor 框架做一个工程时, 它会把所有文件包在一起, 但似乎没有一种方法可以明确声明“ 我希望这个文件在它之前被装入 ” 。

例如, s 例如, 我有两个 javascript 文件 : foo.js bar.js

文件 bar.js 实际含有代码,取决于 foo.js 内部的代码,但Meteor在 bar.js 之前装入了 foo.js ,打破了工程。

  • In node.js I would simply use require( ./bar ) inside foo.js
  • In the browser, I would put a <script> tag pointing to foo.js and another, after, pointing to bar.js, in order to load the files in the correct order.

我们怎样才能在“坚固的气象”

最佳回答

根据Meteor文件,目前以此顺序装入文件 :

  1. Files in [project_root]/lib are loaded first
  2. Files are sorted by directory depth. Deeper files are loaded first.
  3. Files are sorted in alphabetical order.
  4. main.* files are loaded last.

Source: http://docs.meteor.com/#structuringyourapp

问题回答

不是所有情景的解决方案, 但我认为任何依赖于其他代码的东西 最好都会被放置在 Meteor. 启动功能中, 以确保所有的东西都已经装入 。

你总能给我们一个像的JS装载器。"http://yepnopejs.com/" rel="nofollow">yepnope.js 。这对我有用。

我有一套通用功能,我在通用命名空间(js Global)下设置了这些功能。

一.e.

// utils/utils.js
Utils = {};

然后在子文件夹中:

// utils/validation/validation.js
Utils.Validation = {};

// utils/validation/creditCard.js
Utils.Validation.creditCard = ... // validation logic etc

我还有一堆代码 使用Utils 和它的子对象。

显然,这一结构在Meteor首先装载子文件夹时不起作用。

为了让它如预期的那样发挥作用,我不得不创建无意义名称的/子文件夹/子文件夹/子文件夹/子文件夹,然后在最深的子文件夹中插入根对象,在子文件夹中插入分支对象,而分支对象则不那么深。

这对我的品味和差错易感(假设你有在文件夹结构中更深层的成分)来说是极为反直观的。

为了解决这个问题,我利用Q图书馆延期并承诺。 解决方案仍然不干净,因为它使您常规代码重复和检查,但它能让您完全控制负荷订单,而不会干扰目录结构(向那些说您可以随意组织流星代码的人致以问候 ) 。

示例:

//utils.js
UtilsDefer = UtilsDefer || Q.defer();
UtilsDefer.resolve({
    // here some root utils stuff
});

//cards.js
// here we ll depend on Utils but don t want to care about directory structure
UtilsDefer = UtilsDefer || Q.defer(); // it will be a) already 
// resolved defer from utils.js, or b) new defer that will
// be resolved later in utils.js
UtilsDefer.then(function(Utils) {
    // do something with utils usage, or for instance add some fields here
    Utils.CreditCardDefer = Utils.CreditCardDefer || Q.defer();
    Utils.CreditCardDefer.resolve({
        // Credit card utils here
    })
});

//someOtherFile.js
// it will be pain to use sub-objects with this method though:
UtilsDefer = UtilsDefer || Q.defer();
UtilsDefer.then(function(Utils) {
    Utils.CreditCardDefer = Utils.CreditCardDefer || Q.defer();
    Utils.CreditCardDefer.then(function(CreditCard) {
        // do stuff with CreditCard _if_ you need to do it on startup stage   
    })
});

这是相当狭义使用案例的例子, 因为大多数情况下, 您会乐于在某些用户互动回调或 Meteor. startup 中处理这些全局。 否则, 如果您想要在很早的阶段精细控制初始化命令, 这可能是个解决方案 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签