{ "name": "sumatra", "version": "0.0.0", "main": "./pkg/sumatra.js", "dependencies": { "jquery": "~1.9.1", "underscore": "~1.4.4", "bootstrap.css": "~2.1.1" }, "devDependencies": { "coffee-script": "~1.6.2" }, "ignore": [ "components/**", "test/**", "lib/**", "package.json", "index.html" ], "gitHead": "c7f52b1ad87c6d4c516eafc8307d8ecb5b132e9b", "readme": "# Sumatra\n\nSumatra is a CoffeeScript framework for writing jQuery plugins harder,\nbetter, faster, stronger.\n\nYou should use Sumatra if you...\n\n- Encapsulate complex jQuery plugins in a service object and call an\n instance of that service object for each DOM element the plugin\n selector is passed\n- Enjoy test-driven development, clear code, and convention over\n configuration\n- Believe unicorns are real\n\n## Why?\n\nA lot of jQuery plugins are written to encapsulate a simple bit of\nfunctionality used throughout the application. But jQuery's syntax was\ndesigned to improve the way people write JavaScript. CoffeeScript has a\nsimilar goal, but approaches it from a different angle, it compiles its\nsyntax into JavaScript but does so in a safe, syntactically correct and\n(mostly) readable way. This framework unites the two, and finally allows\njQuery developers to build plugins in CoffeeScript without making their\ncode look, well, downright ugly!\n\n## Installation\n\n### Requirements\n\n- jQuery\n- CoffeeScript if you want to develop it..\n\nWe recommend Bower for installing Sumatra as a component:\n\n```bash\n$ bower install sumatra\n```\n\nHowever, you can also install Sumatra manually by just including the\n`pkg/sumatra.js` file in your javascripts directory.\n\n## Usage\n\nSumatra values convention over configuration, and its usage revolves\naround an established pattern that hopefully others will find useful.\n\n### Defining a Basic Plugin\n\nAfter loading Sumatra, you can build jQuery plugins that are both clear\nand superbly terse:\n\n```coffeescript\nsumatra 'clickMe', ->\n class ClickMe extends SumatraPlugin\n action: 'click'\n perform: (event) =>\n element_id = @element.attr('id') || '
'\n alert \"You just clicked #{element_id}!\"\n```\n\nAll this plugin does is show an `alert()` when the element is clicked.\nYou can define a single action with `action:` and then define the\n`perform()` event handler that binds to whatever action you've set\non the element.\n\nTo bind an element to this event, just call it like any normal\njQuery plugin:\n\n```coffeescript\n$('#my_element').clickMe();\n```\n\n### Parameters\n\nYou can also make plugins that pass in options. All Sumatra plugins\ntake an `options` hash as their only argument, regardless of whether\nthe service object uses them or not.\n\n```coffeescript\nsumatra 'ajaxSubmit', ->\n class AjaxSubmit extends SumatraPlugin\n action: 'submit'\n mergeOptions: ->\n @defaults = @_getFormDefaults()\n _.extend(@options, @defaults)\n\n perform: (event) =>\n event.preventDefault()\n event.stopPropagation()\n $.ajax @options\n\n _getFormDefaults: ->\n {\n url: @element.attr('action')\n type: @element.attr('method')\n error: (message, status, xhr) ->\n console.log status, message, xhr\n alert \"#{status}: #{message}\"\n }\n```\n\nThis is an example of [ajaxSubmit from the jQuery.form plugin][jqform],\nimplemented using Sumatra. It would especially be useful when rendering\nan inline response with JSON, using something such as Handlebars to\ncompile the JSON data into a logic-less client-side template...\n\n```coffeescript\n$('form').ajaxSubmit \\\n dataType: 'json'\n success: (context) =>\n template = Handlebars.compile $('#response_template')\n response = template(context)\n @element.html response\n```\n\n### Basic Properties\n\nAs a by-product of the jQuery instantation process, each `SumatraPlugin`\ncomes with the following three properties, for free:\n\n- **element:** References a single jQuery DOM Object, which can perform\n basic functionality on the page. It is obtained from the collection of\n objects which matched the plugin's selector upon instantiation.\n- **index:** The index of the jQuery DOM Object in the collection of\n objects which matched the plugin's selector upon instantiation.\n- **options:** A Hash-notated Object obtained as the only argument in\n the jQuery plugin when instantiated. This object is then merged with\n the `defaults` hash, which are default params in the plugin's\n definition, before initialization occurs.\n\n### Workflow\n\nEach SumatraPlugin has a \"workflow\" that is expressed as a series of\nmethods, all run in the `constructor` of the object. The constructor\nis responsible for setup of the object's basic properties. This method\nshould never be overridden, instead, each step of the instantiation\nprocess can be controlled by overriding one of the following methods:\n\n- **mergeOptions:** Merge the options with the defaults hash. You can\n override this to use attributes from `@element` as defaults instead.\n- **initialize:** The main override of the constructor method, this is\n where one would actually \"construct\" the objects they are going to be\n using in this plugin instance, bind events, and call helper methods.\n- **bindEvents:** This is where the `action:` event should be bound in\n some way. In many cases, this is overridden to bind other events as\n well as the `action:`, or binding the event as a `$(document).on`.\n- **perform:** The event handler of the plugin, this is normally called\n when the `action:` event is fired, but it must be defined if it is\n called or it will throw an error.\n\nYou can define more methods, but these are the only public methods that\nshould be overridden. Any method beginning with `_` is considered\n\"private\" and should not be overridden. Please carry this convention\nto your own code as well.\n\n## Development\n\nYou can build this code into JavaScript by running the following\ncommand at the root dir:\n\n```bash\n$ npm install && cake build\n```\n\n### Contributions\n\nContributions will be accepted via Git/GitHub pull requests, as long as\nyou write tests that prove your contributions work. We use Jasmine to\nwrite tests in CoffeeScript (you know, for the actual framework?) and\nRSpec to write tests for the Rails helpers.\n\n### Releases\n\nAll releases will be made in both CoffeeScript and JavaScript, and\navailable simultaneously on the Bower and RubyGems package managers.\nWe use Bower to manage the standalone JavaScript code which has no\ndependency on Sprockets, Rails, or anything Ruby.\n\nThis code is released under the [MIT License][LICENSE].\n\n[jqform]: http://jquery.malsup.com/form\n[LICENSE]: https://github.com/tubbo/sumatra/blob/master/LICENSE.md\n[engine]: http://github.com/tubbo/sumatra-rails\n", "readmeFilename": "README.md", "_id": "sumatra@0.0.4", "description": "Sumatra is a CoffeeScript framework for writing jQuery plugins harder, better, faster, stronger.", "commit": "c7f52b1ad87c6d4c516eafc8307d8ecb5b132e9b", "repository": { "type": "git", "url": "git://github.com/tubbo/sumatra.git" } }