-
Notifications
You must be signed in to change notification settings - Fork 1
Motivation
We take the awesomeness of CSS for granted. In CSS, when you apply a style to a selector, it always works and always applies to matching DOM nodes - no matter when those nodes were created.
JS does not work this way: if you attach event handlers to nodes matching
'.i_am_cool' now, future i_am_cool nodes won't necessarily get those handlers.
We can fix handlers with event delegation (as in jQuery).
That works fine for event handlers, but it can't help you when your Javascript needs to make changes to the document being displayed to the user. This is increasingly important when you want your Javascript to degrade gracefully, since you often want the pre-Javascript HTML to look one way, but have the Javascript modify it when it loads.
Suppose, for example, we want to unobtrusively add structure to some divs (i.e. rounded corners) or replace one element with another (convert forms to links or other behavior). These transformations will get run once when our script executes, and won't happen automatically to future matching elements.
NinjaScript allows you to specify behaviors - including transformations - and attach them to selectors once, and then count on them always applying to any future element that matches those selectors.
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.