-
Notifications
You must be signed in to change notification settings - Fork 1
Home
NinjaScript is a jQuery library designed to allow unobstrusive scripting simply and easily. Essentially, we use jQuery selectors to apply behavior the same way we use CSS stylesheets to apply stylings. Additionally, NinjaScript makes it easy to package up useful behaviors and apply them quickly to disparate elements, or in different projects.
NinjaScript allows you to specify behaviors - including "transforms" (arbitrary code) - and attach them to selectors once. Those behaviors will then apply to all current and future elements that match that selector. The "transform" code will get run only once per matching item, as soon as it is inserted into the page.
It also lets you package up named behaviors for reuse, and NinjaScript includes a bunch of predefined packaged behaviors for common utilities like AJAX submission and handling graceful degradation cases.
Ninja.orders(function(Ninja){ // boilerplate
Ninja.behavior({
"#mail form.new_mail": Ninja.submitsAsAjax,
"#message_list .item": {
transform: function(elem) { $(elem).doSomethingCool() },
click: function(event) { ... click handler ...},
mouseover: function(event) { ... mouseover handler ... }
}
}
}
The first behavior uses a predefined Ninja.submitsAsAjax behavior to transform all current and future items matching "#mail form.new_mail" into AJAX forms.
In the second behavior, all current and future elements on the page that match "#message_list .item" will have bound event handlers for click and mouseover. In addition, the transform function will get called once for each currently matching DOM node, and once for each future matching DOM node.
First: see the Tutorial or the Reference.
More details on the reasoning behind NinjaScript:
To contribute to NinjaScript, fork it on GitHub and issue pull requests. Make sure you run the tests; this can be accomplished just by loading the file "SpecRunner.html" in a browser, locally. All the test libraries necessary are hosted on http://js-testing.lrdesign.com, so it should just work.