erichmenge / handlers-js

Easy, modular UJS for your Rails apps
MIT License
29 stars 3 forks source link

Handlers-js Build Status

Handlers-js provides an easy to use, dead simple, unobtrusive Javascript modularization through the Rails asset pipeline.

What's that mean? That means all you need to do is register your handler, and then call it with data-handler="MyHandler" on your HTML tags.

Features

Installation

Add this line to your application's Gemfile:

gem 'handlers-js'

And then execute:

$ bundle

Usage

CoffeeScript is provided via the asset pipeline. A single class called Handlers is created. Your asset manifest should look something like this:

//= require turbolinks
//= require handlers
//= require_tree .

IMPORTANT If you're using Turbolinks, make sure it is included first so that Handlers-js knows about it.

Now, simply create your CoffeeScript files like so:

class TypeAhead
  constructor: (el) ->
    url       = $(el).data('typeahead-url')
    property  = $(el).data('typeahead-property')
    value     = $(el).data('typeahead-value')

    $(el).typeahead
      source: (typeahead, query) ->
        $.ajax
          dataType: 'json'
          url: url + query
          success: (data) ->
            typeahead.process(data)
      property: property
      onselect: (val) ->
        $(el).val(val.name)
        $(el).closest('form').submit()

  destroy: ->
    doSomeCleanup()

Handlers.register 'TypeAhead', TypeAhead

Then, in your HTML:

<span data-handler="TypeAhead" data-typeahead-url="..." data-typeahead-property="..." data-typeahead-value="...">
  ...
</span>

Need to use more than one Handler on a particular element? No problem. They are comma separated.

<span data-handler="TypeAhead,SomethingSpecial" ...></span>

If you're not using Turbolinks

If you're not using Turbolinks, fear not. Handlers are still easy to use.

Handlers responds to three triggers:

A pjax example:


$(document).on 'pjax:start', ->
  $(document).trigger 'handlers:pageUpdating', '[data-pjax-container]'

$(document).on 'pjax:end', ->
  $(document).trigger 'handlers:pageUpdated', '[data-pjax-container]'

$ ->
  $(document).trigger 'handlers:pageChanged'

Not using any AJAX + Pushstate library but still want the benefit of modular Javascript? Simply put this in a CoffeeScript file:

$ ->
  $(document).trigger 'handlers:pageChanged'

Contributing

  1. Fork it
  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 new Pull Request