• <i id='KwQxa'><tr id='KwQxa'><dt id='KwQxa'><q id='KwQxa'><span id='KwQxa'><b id='KwQxa'><form id='KwQxa'><ins id='KwQxa'></ins><ul id='KwQxa'></ul><sub id='KwQxa'></sub></form><legend id='KwQxa'></legend><bdo id='KwQxa'><pre id='KwQxa'><center id='KwQxa'></center></pre></bdo></b><th id='KwQxa'></th></span></q></dt></tr></i><div id='KwQxa'><tfoot id='KwQxa'></tfoot><dl id='KwQxa'><fieldset id='KwQxa'></fieldset></dl></div>

    <small id='KwQxa'></small><noframes id='KwQxa'>

      1. <tfoot id='KwQxa'></tfoot><legend id='KwQxa'><style id='KwQxa'><dir id='KwQxa'><q id='KwQxa'></q></dir></style></legend>

          <bdo id='KwQxa'></bdo><ul id='KwQxa'></ul>

      2. 一组可供 Rails 应用程序使用的 CoffeeScript/JavaScript 类和方法

        Set of CoffeeScript/JavaScript classes and methods available to rest of Rails app(一组可供 Rails 应用程序使用的 CoffeeScript/JavaScript 类和方法)

          <legend id='X1Svh'><style id='X1Svh'><dir id='X1Svh'><q id='X1Svh'></q></dir></style></legend>
          • <bdo id='X1Svh'></bdo><ul id='X1Svh'></ul>

              • <small id='X1Svh'></small><noframes id='X1Svh'>

                  <tbody id='X1Svh'></tbody>
              • <i id='X1Svh'><tr id='X1Svh'><dt id='X1Svh'><q id='X1Svh'><span id='X1Svh'><b id='X1Svh'><form id='X1Svh'><ins id='X1Svh'></ins><ul id='X1Svh'></ul><sub id='X1Svh'></sub></form><legend id='X1Svh'></legend><bdo id='X1Svh'><pre id='X1Svh'><center id='X1Svh'></center></pre></bdo></b><th id='X1Svh'></th></span></q></dt></tr></i><div id='X1Svh'><tfoot id='X1Svh'></tfoot><dl id='X1Svh'><fieldset id='X1Svh'></fieldset></dl></div>
                  <tfoot id='X1Svh'></tfoot>
                  本文介绍了一组可供 Rails 应用程序使用的 CoffeeScript/JavaScript 类和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm using Rails 3.2.9. When I add CoffeeScript code to a .js.coffee file in the /app/assets/javascripts directory, I get the resulting JavaScript in all of my webpages. The problem is all the JavaScript is wrapped in:

                  (function() {
                    // my code
                  }).call(this);
                  

                  So any methods I define are not visible in any other CoffeeScript code I write in other files. What's the proper way to write a set of reusable CoffeeScript classes and methods with Rails?

                  解决方案

                  The simplest thing to do is to namespace all your classes. If your application is called "app" then in your initialization code before anything else happens:

                  // Set up the namespace.
                  window.app = { }
                  

                  and then in all your .coffee files:

                  class app.Pancakes
                      #...
                  

                  Then you'd have a global namespace and you'd reference everything through that namespace:

                  pancakes = new app.Pancakes
                  

                  Similarly for simple functions:

                  app.where_is = (pancakes, house) -> ...
                  
                  # And elsewhere...
                  x = app.where_is(...)
                  

                  There are various ways of setting up and partially hiding the namespace but they're all variations on the above and simple namespacing plays nicely with the Rails asset pipeline.

                  这篇关于一组可供 Rails 应用程序使用的 CoffeeScript/JavaScript 类和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

                          <tbody id='ZFpwu'></tbody>

                        <i id='ZFpwu'><tr id='ZFpwu'><dt id='ZFpwu'><q id='ZFpwu'><span id='ZFpwu'><b id='ZFpwu'><form id='ZFpwu'><ins id='ZFpwu'></ins><ul id='ZFpwu'></ul><sub id='ZFpwu'></sub></form><legend id='ZFpwu'></legend><bdo id='ZFpwu'><pre id='ZFpwu'><center id='ZFpwu'></center></pre></bdo></b><th id='ZFpwu'></th></span></q></dt></tr></i><div id='ZFpwu'><tfoot id='ZFpwu'></tfoot><dl id='ZFpwu'><fieldset id='ZFpwu'></fieldset></dl></div>
                        <tfoot id='ZFpwu'></tfoot>
                        • <small id='ZFpwu'></small><noframes id='ZFpwu'>

                        • <legend id='ZFpwu'><style id='ZFpwu'><dir id='ZFpwu'><q id='ZFpwu'></q></dir></style></legend>
                            <bdo id='ZFpwu'></bdo><ul id='ZFpwu'></ul>