这个 JavaScript 代码片段有什么作用?
这是一个简短的问题,我还没有通过谷歌找到正确的答案。 好吧,我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
呃...它运行函数
window.Application.init
,该函数可能附加到其他一些 Coffeescript 文件中的window
。查找包含class window.Application
或window.Application =
的文件。Erm... it runs the function
window.Application.init
which was probably attached to thewindow
in some other coffeescript file. Look for a file containingclass window.Application
orwindow.Application =
.回答您的后续问题:
有效地将本地参考“应用程序”提升为全球符号。浏览器中的
window
对象是全局范围,因此全局变量是对window
属性的引用。因此,创建window
的属性并将其设置为某个值(一个对象,在本例中可能是一个函数;我对 Coffeescript 不太了解)使该值全局可用。To answer your follow-on question:
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 ofwindow
. Therefore, creating a property ofwindow
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.它启动使用 CoffeeScript 构建的应用程序。查看应用程序的初始化程序并逐步调试它。
It starts the application which is built with coffeescript. Look at the initializer of the application and debug it step by step.