【发布时间】:2014-07-26 23:52:52
【问题描述】:
有如下问题:我在shared.js.coffee 模块中有一些共享的JS 函数。我想使用这些函数,例如在模块 some_actions.js.coffee 中。所以,但我不知道我应该如何正确共享第一个模块中的功能。请给我建议,把它做好。谢谢。
【问题讨论】:
标签: javascript ruby-on-rails coffeescript
有如下问题:我在shared.js.coffee 模块中有一些共享的JS 函数。我想使用这些函数,例如在模块 some_actions.js.coffee 中。所以,但我不知道我应该如何正确共享第一个模块中的功能。请给我建议,把它做好。谢谢。
【问题讨论】:
标签: javascript ruby-on-rails coffeescript
在这些情况下,最好使用 Paloma gem (https://github.com/kbparagua/paloma) 并在需要时使用它,例如,像这样使用它:
gem 'paloma'
然后您可以在application.js or shared.js.coffee 中编码,但请确保您有
//= require shared.js.coffee
在您的 application.js 中。然后你会这样做:
var UsersController = Paloma.controller('Users');
// Executes when Rails User#new is executed.
UsersController.prototype.new = function(){
alert('Hello User!' );
};
在您的控制器中:
def UsersController < ApplicationController
def new
# a Paloma request will automatically be created.
@user = User.new
end
end
我认为这应该有助于您理解。
【讨论】: