Coffeescript,Backbone和加载顺序 [英] Coffeescript, Backbone and load order

查看:168
本文介绍了Coffeescript,Backbone和加载顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个Coffeescript类,在每个类都存在于自己的文件中的应用程序中。

 类管理器扩展人
title:[[manager]]

如果该文件在titles ,生成错误。我假设这是因为Coffeescripts安全包装是执行.call(this),当这个文件第一次加载?



否则,如果我延迟运行任何代码,直到整个页面完全加载($(document.ready()),我可以确保所有的javascript文件在任何代码实际运行之前完全加载。


$ b $

解决方案

这可能会导致一些恼人的加载顺序问题,


















$ b

如果 titles 在另一个文件中定义,则 class Manager 的范围没有影响,这是一个订单问题。 titles 如何定义?


运行任何代码,直到整个页面完全加载($(document.ready()),我可以确保所有的javascript文件在任何代码实际运行之前完全加载。




不完全。 $(document).ready()(注意括号 - 没有 document.ready function ...)延迟函数的执行,直到所有页面的HTML都已加载,这并不意味着所有的JavaScript都已加载。这里是一个好消息:从JavaScript代码的角度来看,无论是否已加载其他JavaScript文件,因为它们都按顺序运行。 (注意:我假设您没有在JavaScript代码中添加其他< script> 代码)只要你有

 < script src =titles.js>< / script> 
< script src =Manager.js>< / script>

您可以放心 Manager.js 将只在 titles.js 有。



警告! c $ c> $(document).ready()用于排序JS代码是一个常见的错误,可能导致混乱!如果您的HTML看起来像这样

 < script src =Manager.js>< / script& 
< script src =titles.js>< / script>

其中 titles.js titles Manager.js 看起来像这样

  $(document).ready  - > 
console.log titles

那么输出将有时 titles 有时 undefined 。为什么?因为文档说,


如果 .ready()在DOM初始化后调用,则传入的新处理程序将立即执行。


当第一个JS文件运行时,DOM可能已经被初始化! (实际上,如果浏览器将网页的HTML缓存,则会发生这种情况。)



因此,保持简单。只需按正确的顺序加载脚本即可。请记住,出于所有实际目的,您的脚本将由浏览器按顺序连接到一个JS文件中。


Consider this Coffeescript class, in an app where each class lives in its own file.

class Manager extends Person
  title: titles["manager"]

If that file is loaded before the "titles" object, an error generated. I'm assuming this is because of Coffeescripts safety wrapper which is performing ".call(this)" when this file is first loaded?

Otherwise, if I were to delay running any code until after the entire page had fully loaded ($(document.ready()), I could be sure that all the javascript files were fully loaded before any code actually ran.

Doesn't this create some annoying load order problems, or am I not doing something correctly?

解决方案

It can't be both an order issue and a wrapper issue. Wrapping something with a function that's run immediately has no effect on order, only on scope.

And if titles is defined in another file, then the scoping of class Manager doesn't matter. So, it's an order issue. How is titles defined?

if I were to delay running any code until after the entire page had fully loaded ($(document.ready()), I could be sure that all the javascript files were fully loaded before any code actually ran.

Not quite. $(document).ready() (note the parentheses—there is no document.ready function...) delays a function's execution until all of the page's HTML has been loaded, which doesn't mean all JavaScript has been loaded. Here's the good news: From the standpoint of JavaScript code, it doesn't matter whether other JavaScript files have been loaded, because they're all run in order. (Note: I'm assuming here that you're not doing anything fancy like adding additional <script> tags from your JavaScript code.) So as long as you have

<script src="titles.js"></script>
<script src="Manager.js"></script>

you can rest assured that Manager.js will only run after titles.js has.

Warning! Relying on $(document).ready() for ordering JS code is a common mistake that can lead to confusion! If your HTML looked like this

<script src="Manager.js"></script>
<script src="titles.js"></script>

where titles.js creates a global called titles and Manager.js looks like this

$(document).ready ->
  console.log titles

then the output will sometimes be titles, and sometimes be undefined. Why? Because as the docs say,

If .ready() is called after the DOM has been initialized, the new handler passed in will be executed immediately.

And the DOM may already have been initialized when the first JS file is run! (In practice, this will tend to happen if the browser has the page's HTML cached.)

So, keep it simple. Just load your scripts in the right order. Remember that, for all practical purposes, your scripts are concatenated together by the browser, in order, into a single JS file.

这篇关于Coffeescript,Backbone和加载顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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