rails_script
所属分类:编程语言基础
开发工具:Ruby
文件大小:0KB
下载次数:0
上传日期:2019-11-05 21:37:03
上 传 者:
sh-1993
说明: RailsScript-一个以Rails为中心、面向对象、轻量级的框架,用于编写CoffeeScript,
(RailsScript - A Rails-centric, object oriented, featherweight framework for writing CoffeeScript,)
文件列表:
Gemfile (97, 2019-11-05)
LICENSE.txt (1062, 2019-11-05)
Rakefile (29, 2019-11-05)
app/ (0, 2019-11-05)
app/assets/ (0, 2019-11-05)
app/assets/javascripts/ (0, 2019-11-05)
app/assets/javascripts/base.coffee (696, 2019-11-05)
app/assets/javascripts/elements/ (0, 2019-11-05)
app/assets/javascripts/elements/.keep (0, 2019-11-05)
app/assets/javascripts/global.coffee (0, 2019-11-05)
app/assets/javascripts/rails_script.coffee (99, 2019-11-05)
app/assets/javascripts/utilities/ (0, 2019-11-05)
app/assets/javascripts/utilities/.keep (0, 2019-11-05)
lib/ (0, 2019-11-05)
lib/generators/ (0, 2019-11-05)
lib/generators/rails_script/ (0, 2019-11-05)
lib/generators/rails_script/class/ (0, 2019-11-05)
lib/generators/rails_script/class/USAGE (0, 2019-11-05)
lib/generators/rails_script/class/class_generator.rb (349, 2019-11-05)
lib/generators/rails_script/class/templates/ (0, 2019-11-05)
lib/generators/rails_script/class/templates/javascript.js.coffee (78, 2019-11-05)
lib/generators/rails_script/controller/ (0, 2019-11-05)
lib/generators/rails_script/controller/USAGE (0, 2019-11-05)
lib/generators/rails_script/controller/controller_generator.rb (354, 2019-11-05)
lib/generators/rails_script/controller/templates/ (0, 2019-11-05)
lib/generators/rails_script/controller/templates/javascript.js.coffee (236, 2019-11-05)
lib/generators/rails_script/element/ (0, 2019-11-05)
lib/generators/rails_script/element/USAGE (0, 2019-11-05)
lib/generators/rails_script/element/element_generator.rb (416, 2019-11-05)
lib/generators/rails_script/element/templates/ (0, 2019-11-05)
lib/generators/rails_script/element/templates/javascript.js.coffee (199, 2019-11-05)
lib/generators/rails_script/install/ (0, 2019-11-05)
lib/generators/rails_script/install/USAGE (0, 2019-11-05)
lib/generators/rails_script/install/install_generator.rb (610, 2019-11-05)
lib/generators/rails_script/utility/ (0, 2019-11-05)
lib/generators/rails_script/utility/USAGE (0, 2019-11-05)
lib/generators/rails_script/utility/templates/ (0, 2019-11-05)
lib/generators/rails_script/utility/templates/javascript.js.coffee (83, 2019-11-05)
... ...
# RailsScript
RailsScript is a Rails-centric, object oriented, featherweight framework for writing CoffeeScript. It is optimized for the [Rails Asset Pipeline](http://guides.rubyonrails.org/asset_pipeline.html) and is compatible with [TurboLinks](https://github.com/rails/turbolinks). Using Rails controller names and actions to call JavaScript, it has never been easier to write clean, concise, and maintainable page specific JavaScript.
## Upgrading to V2
To migrate from version 0.x to 1.x, please see the wiki; https://github.com/gemgento/rails_script/wiki/v2-Migration
## Installation
Add this line to your application's Gemfile:
gem 'rails_script', '~> 2.0'
And then execute:
$ bundle
After bundling you need to run the initial installation generator:
$ rails g rails_script:install
After the generator finishes, you will be prompted to add helper call to your application layout. The generated code is responsible for initializing and calling the action specific JavaScript. This helper should be called before the closing body tag.
```
<%= include_rails_script %>
```
NOTE: Your JS files needed have been included before `include_rails_script`. In other words, you still need `<%= javascript_include_tag "application" %>` in your application layout.
After including the view helper in your application layout, you will need to require the RailsScript javascript library inside your application.js or application.coffee file before the `require_tree .` line.
if using javascript (app/assets/javascripts/application.js):
```
// * other app specific js require lines
//= require rails_script
//= require_tree .
```
if using coffeescript (app/assets/javascripts/application.coffee):
```
# * other app specific js require lines
#= require rails_script
#= require_tree .
```
## Usage
### Page (Action) Specific JavaScript
Your JavaScript class is named after your Controller and there is a method for each Controller action. Whenever you generate a Controller, the CoffeeScript file that is generated will define the new JavaScript class and the basic REST actions. The example below would print 'users#show' in the console for the ```Users#show``` action.
```coffeescript
# app/assets/javascripts/users.js.coffee
class App.Users extends App.Base
show: =>
console.log 'users#show'
```
### Controller Specific JavaScript
Executing some JavaScript to run on all controller actions is just a matter of adding it to the ```beforeAction``` or ```afterAction``` function. ```beforeAction``` is run before all controller action functions and ```afterAction``` is run after all controller action functions. The before and after action functions have an optional argument ```action``` which is a string containing the current action name. The example below would print 'before ACTION action' and 'after ACTION action' for each ```Users``` controller action.
```coffeescript
# app/assets/javascripts/users.js.coffee
class App.Users extends App.Base
beforeAction: (action) =>
console.log "before #{action} action"
afterAction: (action) =>
console.log "after #{action} action"
```
### Application Wide JavaScript
Running some JavaScript on every page of an Application is a common need. For example, we may want to create a site credit rollover in the footer of every page.
```coffeescript
# app/assets/javascripts/base.js.coffee
...
class App.Base
constructor: ->
@footerRollover()
return this
footerRollover: ->
$(".site-credit a").hoverIntent(
over: ->
$(".site-credit a").html("
")
out: ->
$(".site-credit a").html("SITE CREDIT")
)
...
```
In this example we extracted the rollover action into a new function. Doing so will make the class cleaner and easier to maintain as the application grows. Once again note the ```return this``` in the constructor.
### Global Functions
Any functions that need to be accessible in the global scope should be defined in ```global.js.coffee``` using the ```App``` namespace. Below is an example of one of our favorite functions that we use to submit a form using AJAX as a JSON request.
```coffeescript
# app/assets/javascripts/global.js.coffee
App.remoteSubmission = ($form) ->
return $.ajax
url: $form.attr('action')
type: $form.attr('method')
data: $form.serialize()
dataType: 'json'
```
Now you can access this function from anywhere in the application by just calling ```App.remoteSubmission($('#myForm'))```
### Utilities
A ```Utility``` is a class that will be used to create similar functionality in many areas of the application. A good example of this is a Modal, which could appear multiple times on the same page. So, let's encapsulate this functionality in a highly reusable class.
First, generate the ```Utility```
$ rails g rails_script:utility Modal
This will create the following in ```/app/assets/javascripts/utilities/modal.js.coffee```:
```coffeescript
# /app/assets/javascripts/utilities/modal.js.coffee
class Utility.Modal
constructor: ->
return this
```
Let's add some basic functionality:
```coffeescript
# /app/assets/javascripts/utilities/modal.js.coffee
class Utility.Modal
isOpen: false
constructor: ($element, $trigger) ->
@element = $element
@trigger = $trigger
@trigger.on 'click', @toggle
return this
toggle: (event) =>
event.preventDefault()
if @isOpen then @close() else @open()
open: =>
@isOpen = true
@element.show()
close: =>
@isOpen = false
@element.fadeOut('fast')
```
Now, here's how we use the utility from ```users#show```
```coffeescript
# app/assets/javascripts/users.js.coffee
class App.Users extends App.Base
show: ->
@galleryModal = new Utility.Modal($('#user-gallery-modal-wrapper'), $('user-gallery-modal-toggle-button'))
```
### Elements
An ```Element``` is a class that describes the functionality of a one off element in the application. A Main Menu is a good example of this since there is usually only a single Main Menu.
First generate the ```Element```
$ rails g rails_script:element MainMenu
This will create the following in ```/app/assets/javascripts/elements/main_menu.js.coffee```
```coffeescript
# /app/assets/javascripts/elements/main_menu.js.coffee```
class Element.MainMenu
constructor: ->
return this
```
We can now add all the logic for the main menu in a separate class and call it on every page like so:
```coffeescript
# app/assets/javascripts/base.js.coffee
class App.Base
constructor: ->
App.mainMenu = new Element.MainMenu()
return this
```
#### Element Inheritance
Inheritance is another key tool for reusability. Let's say our ```Element.MainMenu``` opens and closes in the same way as the ```Utility.Modal```. Well then MainMenu should just extend Modal, this can be accomplished from the generator:
$ rails g rails:script MainMenu Modal
Which generates:
````coffeescript
# /app/assets/javascripts/elements/main_menu.js.coffee
class Element.MainMenu extends Utility.Modal
constructor: ->
return this
````
Inheritance from the generator can only come from a Utility class. Any class you wish to extend should be created as a Utility. The installer adds the line ```//= require_tree ./utilities``` before loading tree to handle this. If you have a utility that extends a utility, then make sure the extended utility is loaded first by explicitly requiring it before ```//= require_tree ./utilities```.
### Custom Controllers
When a new controller is generated, the JavaScript asset file will be generated with App. However, if you need to manually generate a RailsScript controller you can use:
$ rails g rails_script:controller Some::NewController
Since the above example includes a namespace, it would generate:
```coffeescript
# app/assets/javascripts/some/new_controller.js.coffee
window.App ||= {}
class App.SomeNewController extends App.Base
beforeAction: (action) =>
return
afterAction: (action) =>
return
index: =>
return
show: =>
return
new: =>
return
edit: =>
return
```
None of the pre-defined functions are necessary, you can remove the ones you don't need.
### Generic Classes
To generate a generic class that isn't a Utility, Element or Controller, just use the following:
$ rails g rails_script:class My::ClassName
Which generates:
```coffeescript
# /app/assets/javascripts/my/class_name.js.coffee
class App.MyClassName
constructor: ->
return this
```
### Passing Rails Variables
To pass data from Rails to JavaScript, just call ```to_javascript``` along with a hash of your data. This is then converted to a JSON object with ```to_javascript.to_json``` and can be accessed with ```Utility.RailsVars```. The ```to_javascript``` helper may be called from multiple points in the application, all data is merged together.
Here's an example where ```to_javascript``` is used in a ```before_filter``` to pass the current user and their friends:
```ruby
# /app/controllers/application_controller.rb
class ApplicationController
before_filter :set_javascript_vars
private
def set_javascript_vars
to_javascript user: current_user, friends: current_user.friends
end
end
```
And here's how we print that data to the console on the ```users#index``` action:
```coffeescript
# /app/assets/javascripts/users.js.coffee
class App.Users extends App.Base
index: =>
console.log Utility.RailsVars.user
console.log Utility.RailsVars.friends
```
### Events
Since Turbolinks doesn't refresh the page and only replaces the body, event listeners defined on ```window``` and ```document``` carry between page loads. To avoid these event listeners stacking, RailsScript will destroy all event listeners on ```window``` and ```document``` that have a blank namespace, i.e. ```$(window).on 'scroll', myHandler```. If you need an event handler to persist between page changes, then define a namespace, i.e. ```$(window).on 'scroll.namespace', myHandler```.
### Page Transitions
Full page transitions are super easy with RailsScript and Turbolinks. Checkout the wiki for more information on how to add these to your RailsScript application, https://github.com/gemgento/rails_script/wiki/Turbolinks-Page-Transitions.
## Contributing
1. Fork it ( https://github.com/[my-github-username]/rails_script/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
近期下载者:
相关文件:
收藏者: