这个 JavaScript 代码片段有什么作用?

发布于 2024-12-17 03:18:33 字数 1586 浏览 1 评论 0原文

这是一个简短的问题,我还没有通过谷歌找到正确的答案。 好吧,我有这个 Rails 项目,其中有很多 JavaScript 文件(实际上是 CoffeeScript),看起来让事情顺利进行的代码是这样的...

index.html.haml

:javascript
  $(document).ready(function () {
    window.Application.init()
  });

就这些了在里面,我不知道该行: window.Application.init() 是做什么的?你们能给我解释一下吗?

我认为这非常重要,因为该项目主要是一堆 coffescript 文件,几乎没有任何服务器端处理。

提前致谢!

编辑:(我找到了应用程序类的代码)

class Application
  # Creates the map by using the geolocation center
  # Returns a deferred promise with the bounds
  setup = (position) ->
    deferred = new $.Deferred()
    Ext.setup
      glossOnIcon: false
      onReady: ->
        Application.mapPanel = new MapPanel(position, deferred)
        Application.loading = new Ext.LoadMask( Ext.getBody(), { msg: "Loading..." } )

    deferred.promise()

  # Handles the geolocation error.
  @onFail = ->
    Ext.setup
      onReady: ->
        new Ext.Panel
          fullscreen: true,
          dockedItems: [],
          items: []

        Ext.Msg.confirm "Position Unavailable",
          "Can not confirm your location. Would you like to go to the instructions page?",
          (button) ->
            document.location = if button == "no" then "/posts" else "/instructions"

  @init = ->
    Geolocation.onStart = (position) ->
      $.when(setup(position)).pipe(Post.latest).then (data) ->
        Application.mapPanel.paintPosts data

    Geolocation.onUserRejected = @onFail
    Geolocation.init()

window.Application = Application

This is a short question on account that I haven't found the proper answer by using Google.
Ok, I have this Rails project which has a lot of JavaScript files (CoffeeScript Actually), and it appears that the code that gets the balls rolling is this...

index.html.haml

:javascript
  $(document).ready(function () {
    window.Application.init()
  });

That's all there is inside, I have no idea what does the line: window.Application.init() do? Could you guys please explain it to me?

I'm assuming this is very important since the project is mostly a bunch of coffescript files, and hardly any server-side processing.

Thanks in advance!

EDIT: (I found the code for the Application class)

class Application
  # Creates the map by using the geolocation center
  # Returns a deferred promise with the bounds
  setup = (position) ->
    deferred = new $.Deferred()
    Ext.setup
      glossOnIcon: false
      onReady: ->
        Application.mapPanel = new MapPanel(position, deferred)
        Application.loading = new Ext.LoadMask( Ext.getBody(), { msg: "Loading..." } )

    deferred.promise()

  # Handles the geolocation error.
  @onFail = ->
    Ext.setup
      onReady: ->
        new Ext.Panel
          fullscreen: true,
          dockedItems: [],
          items: []

        Ext.Msg.confirm "Position Unavailable",
          "Can not confirm your location. Would you like to go to the instructions page?",
          (button) ->
            document.location = if button == "no" then "/posts" else "/instructions"

  @init = ->
    Geolocation.onStart = (position) ->
      $.when(setup(position)).pipe(Post.latest).then (data) ->
        Application.mapPanel.paintPosts data

    Geolocation.onUserRejected = @onFail
    Geolocation.init()

window.Application = Application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

涫野音 2024-12-24 03:18:33

呃...它运行函数 window.Application.init ,该函数可能附加到其他一些 Coffeescript 文件中的 window 。查找包含class window.Applicationwindow.Application = 的文件。

Erm... it runs the function window.Application.init which was probably attached to the window in some other coffeescript file. Look for a file containing class window.Application or window.Application =.

眼泪也成诗 2024-12-24 03:18:33

回答您的后续问题:

window.Application = Application

有效地将本地参考“应用程序”提升为全球符号。浏览器中的window对象是全局范围,因此全局变量是对window属性的引用。因此,创建 window 的属性并将其设置为某个值(一个对象,在本例中可能是一个函数;我对 Coffeescript 不太了解)使该值全局可用。

To answer your follow-on question:

window.Application = Application

effectively promotes the local reference "Application" to a global symbol. The window object in the browser is the global scope, and global variables are references therefore to properties of window. Therefore, creating a property of window and setting it to some value (an object, maybe a function in this case; I don't know much about Coffeescript) makes that value available globally.

浮生未歇 2024-12-24 03:18:33

它启动使用 CoffeeScript 构建的应用程序。查看应用程序的初始化程序并逐步调试它。

It starts the application which is built with coffeescript. Look at the initializer of the application and debug it step by step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文