连接后将所有CoffeScript文件包含在JQuery文档就绪函数中? [英] Wrap all CoffeScript files in a JQuery document ready function after concatenation?

查看:306
本文介绍了连接后将所有CoffeScript文件包含在JQuery文档就绪函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些CoffeeScript文件,我在转换为Javascript之前使用Cakefile连接。如何使用JQuery在文档就绪函数上包装连接的CoffeScript文件?

I have a number of CoffeeScript files which I concatenate using a Cakefile before converting to Javascript. How do I wrap the concatenated CoffeScript file with a JQuery on document ready function?

例如:

有以下CoffeScript文件,每个文件包含一个类:

I have the following CoffeScript files each containing a Class:

foo.coffee
bar.coffee
tea.coffee

foo.coffee bar.coffee tea.coffee

为了生成JS文件,我使用以下Cake命令来连接咖啡并生成JS:

To produce the JS file I use the following Cake command to concatente the coffee and produce the JS:

task 'build', 'Build single application file from source files', ->
  appContents = new Array remaining = appFiles.length
  for file, index in appFiles then do (file, index) ->
    fs.readFile "#{file}.coffee", 'utf8', (err, fileContents) ->
      throw err if err
      appContents[index] = fileContents
      process() if --remaining is 0
  process = ->
    fs.writeFile '../assets/js/app.coffee', appContents.join('\n\n'), 'utf8', (err) ->
      throw err if err
      exec 'coffee --compile ../assets/js/app.coffee', (err, stdout, stderr) ->
        throw err if err
        console.log stdout + stderr
        fs.unlink '../assets/js/app.coffee', (err) ->
          throw err if err
          console.log 'Done.'

我很好的JS文件像这样:

This produces I nice JS file like so:

// Generated by CoffeeScript 1.3.3
(function() {

   ...all the code...

}).call(this);

我应该做什么来包装..所有的代码..在一个JQuery就绪函数像这样:

What should I do to wrap ..all the code.. in a JQuery on ready function like so:

(function() {

    $(document).ready(function() {
       ...all the code...
    });

}).call(this);

因此,基本上就是在文档准备好后执行的所有连接代码。

So, basically want is all the concatenated code to execute after the document is ready.

子问题是我要求的正确方法吗?是否应该将每个CoffeeScript文件中包含的每个类包含在一个on document ready函数中?

Sub Question Is what I'm asking the correct way to do this? Should I instead wrap each Class contained in each CoffeeScript file in an on document ready function instead?

任何帮助都非常感谢,我已经搜索了如何和低的答案没有效果。

Any help is greatly appreciated, I've searched how and low for an answer to this with no avail.

谢谢!

推荐答案

这里的第一个决定是是否使用coffeescript的隔离模式,你做。这封装了每个coffeescript文件的所有代码。您可能需要在其中显示一些代码,例如:

The first decision here is whether to use coffeescript's isoliation pattern, which you do. This encapsulates all code of each coffeescript file. You might need to expose some of the code inside, like:

foo.coffee

foo.coffee

class Foo
  a: 20
  f: () -> 
      console.log("...")

window.Foo = Foo


b $ b

之后,您可以在每个文档中使用您公开的代码部分,例如:

Afterwards you can use your exposed code parts in every document, like:

$(document).ready(function() {
   var f = new window.Foo();
});

这篇关于连接后将所有CoffeScript文件包含在JQuery文档就绪函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆