ruby-on-rails – 如何在Rails 3.1中使用Sprockets编写DRY,模块
发布时间:2020-12-16 20:33:33 所属栏目:百科 来源:网络整理
导读:我正在尝试写一些明智的Javascript的早期阶段.我想要命名空间基本上是我的应用程序的名称,以尽可能避免全局变量,但仍然给我一种访问在该地方声明的功能的方法.但是,我不想在我的函数定义中超级冗长. 我的理想CoffeeScript将是这样的: class @MyApp @myClass
|
我正在尝试写一些明智的Javascript的早期阶段.我想要命名空间基本上是我的应用程序的名称,以尽可能避免全局变量,但仍然给我一种访问在该地方声明的功能的方法.但是,我不想在我的函数定义中超级冗长.
我的理想CoffeeScript将是这样的: class @MyApp
@myClassMethod = ->
console.log 'This is MyApp.myClassMethod()'
class @Module1
@moduleMethod = ->
console.log 'This is MyApp.Module1.moduleMethod()'
你得到的照片.这样我就避免了写MyApp.Module.submoduleMethod = – >每次我想正确定义一个命名空间的函数 – 使用@和定义我的类定义中的东西保持事物的美好和短. 这一切顺利,直到我想将我的功能分解成多个CoffeeScript文件.那么我真正想要的就是这样的: // application.js
class @MyApp
//= require 'module1'
//= require 'module2'
// module1.js
class @Module1
@moduleMethod = ->
console.log 'This is STILL MyApp.Module1.moduleMethod()'
似乎Sprockets可以做到这一点. 有没有一个明智的方法来要求我的CoffeeScript文件在我的容器文件的正确的地方?或者另一种方法来编写使用CoffeeScript,Sprockets和Rails 3.1分割成单独文件的模块化代码? 解决方法
我有一个我在代码中使用的模块解决方案.
我定义了我的模块,如下所示 @module "foo",->
@module "bar",->
class @Amazing
toString: "ain't it"
惊人的可用作为 foo.bar.Amazing 执行@module帮助器是 window.module = (name,fn)->
if not @[name]?
this[name] = {}
if not @[name].module?
@[name].module = window.module
fn.apply(this[name],[])
这是在咖啡店网站上写的. https://github.com/jashkenas/coffee-script/wiki/Easy-modules-with-coffeescript (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
