Sumatra is a CoffeeScript framework for writing jQuery plugins harder, better, faster, stronger.
You should use Sumatra if you...
A lot of jQuery plugins are written to encapsulate a simple bit of functionality used throughout the application. But jQuery's syntax was designed to improve the way people write JavaScript. CoffeeScript has a similar goal, but approaches it from a different angle, it compiles its syntax into JavaScript but does so in a safe, syntactically correct and (mostly) readable way. This framework unites the two, and finally allows jQuery developers to build plugins in CoffeeScript without making their code look, well, downright ugly!
You came here for docs, right?
We recommend Bower for installing Sumatra as a component:
$ bower install sumatra
However, you can also install Sumatra manually by just including the
pkg/sumatra.js
file in your javascripts directory.
Sumatra values convention over configuration, and its usage revolves around an established pattern that hopefully others will find useful.
After loading Sumatra, you can build jQuery plugins that are both clear and superbly terse:
sumatra 'clickMe', ->
class ClickMe extends SumatraPlugin
action: 'click'
perform: (event) =>
element_id = @element.attr('id') || '<div>'
alert "You just clicked #{element_id}!"
All this plugin does is show an alert()
when the element is clicked.
You can define a single action with action:
and then define the
perform()
event handler that binds to whatever action you've set
on the element.
To bind an element to this event, just call it like any normal jQuery plugin:
$('#my_element').clickMe();
You can also make plugins that pass in options. All Sumatra plugins
take an options
hash as their only argument, regardless of whether
the service object uses them or not.
sumatra 'ajaxSubmit', ->
class AjaxSubmit extends SumatraPlugin
action: 'submit'
mergeOptions: ->
@defaults = @_getFormDefaults()
_.extend(@options, @defaults)
perform: (event) =>
event.preventDefault()
event.stopPropagation()
$.ajax @options
_getFormDefaults: ->
{
url: @element.attr('action')
type: @element.attr('method')
error: (message, status, xhr) ->
console.log status, message, xhr
alert "#{status}: #{message}"
}
This is an example of ajaxSubmit from the jQuery.form plugin, implemented using Sumatra. It would especially be useful when rendering an inline response with JSON, using something such as Handlebars to compile the JSON data into a logic-less client-side template...
$('form').ajaxSubmit \
dataType: 'json'
success: (context) =>
template = Handlebars.compile $('#response_template')
response = template(context)
@element.html response
As a by-product of the jQuery instantation process, each SumatraPlugin
comes with the following three properties, for free:
defaults
hash, which are default params in the plugin's
definition, before initialization occurs.Each SumatraPlugin has a "workflow" that is expressed as a series of
methods, all run in the constructor
of the object. The constructor
is responsible for setup of the object's basic properties. This method
should never be overridden, instead, each step of the instantiation
process can be controlled by overriding one of the following methods:
@element
as defaults instead.action:
event should be bound in
some way. In many cases, this is overridden to bind other events as
well as the action:
, or binding the event as a $(document).on
.action:
event is fired, but it must be defined if it is
called or it will throw an error.You can define more methods, but these are the only public methods that
should be overridden. Any method beginning with _
is considered
"private" and should not be overridden. Please carry this convention
to your own code as well.
You can build this code into JavaScript by running the following command at the root dir:
$ npm install && cake build
Contributions will be accepted via Git/GitHub pull requests, as long as you write tests that prove your contributions work. We use Jasmine to write tests in CoffeeScript (you know, for the actual framework?) and RSpec to write tests for the Rails helpers.
All releases will be made in both CoffeeScript and JavaScript, and available simultaneously on the Bower and RubyGems package managers. We use Bower to manage the standalone JavaScript code which has no dependency on Sprockets, Rails, or anything Ruby.
This code is released under the MIT License.