Statistics
53
4
7d
Badges
Tags
ajax

Naja.js

Build Status Code Coverage npm version npm monthly downloads npm downloads

Naja. a genus of venomous elapid snakes comprising the true cobras. (Also, German for "Well" at the beginning of a sentence.)

Naja is a full-featured JS client-side AJAX library for Nette Framework.

It is written using modern-day JavaScript, but compiled into ES5 build that comes bundled with a few polyfills and thus should work on all modern browsers including IE 10 and above.

Sauce Test Status

iOS builds might be grey, but please don't panic, iOS is fully supported. This is just Saucelabs not being able to setup the test environment for iOS within a generous limit of 2 minutes, causing the jobs to time out occasionally.

Very quick start

$ npm install --save naja
import naja from 'naja';
document.addEventListener('DOMContentLoaded', naja.initialize.bind(naja));

Documentation

You can find the documentation here.

  • 1.5.1 1.5.1

    • Fixed: HistoryHandler now respects naja.defaultOptions.history and does not override it to true when data-naja-history attribute is not set on the element. (Thanks @brosland for investigation!)
  • 1.5.0 1.5.0

    • FormsHandler now optionally accepts a reference to nette-forms (#15). This is handy if you import the nette-forms script manually. If you provide the reference, FormsHandler will use it instead of relying on global window.Nette.
  • 1.4.0 1.4.0

    • Added UIHandler.clickElement() and UIHandler.submitForm() helper methods (#12) that allow you to dispatch UI-bound events manually. This is especially useful if you need to submit a form programmatically (e.g. on change of select box), because form.submit() does not trigger the form's submit event.
      • ⚠️ The helper methods trigger the interaction event with originalEvent set to undefined. This potentially breaks BC if you choose to use the newly introduced methods!
    • Simplified initial pop detection (#10). It doesn't appear to be a problem anymore, but in browsers where it still is a problem (some ancient Chromes and Safaris), this solution should work too while being elegantly simple. As a side effect, this should also prevent popstate behaviour in HistoryHandler being triggered on hashchange.
      • ⚠️ This also removes a few attributes from HistoryHandler that are no longer necessary: initialUrl, initialState, and popped. They were part of the internal API and were never documented, thus I don't consider their removal to be a BC break.
  • 1.3.2 1.3.2

    • Fixed incorrect data-naja-snippet-prepend and data-naja-snippet-append attributes detection in SnippetHandler
  • 1.3.1 1.3.1

    • Fixed incorrect data-naja-history-nocache attribute detection in HistoryHandler (#8, thanks @dakur)
  • 1.3.0 1.3.0

    • You can provide default options for your extensions or even Naja's core components via naja.defaultOptions.
    • The options object is now passed to the success, error, and complete event handlers. Components and extensions can now use the options object to pass metadata along with the request instead of having to keep track of them internally, which should prevent potential concurrency issues.
    • RedirectHandler reads the forceRedirect flag not only from response payload but also from options.
  • 1.2.0 1.2.0

    • SnippetHandler now exposes events that are dispatched prior to and after every snippet update.
    • I got rid of an unnecessary layer of inheritance in a small internal refactoring.
  • 1.1.0 1.1.0

    • Initialization of core services was refactored to allow for further changes.
    • The Promise returned by naja.makeRequest() is resolved with the parsed response body, or rejected with the thrown error. This allows you to chain custom behavior after the response is processed by Naja when calling makeRequest() manually.
    • Aborted requests are handled in a specific abort event. They no longer trigger error handling, as the reasons for aborting the request usually do not indicate an erroneous state.
    • The bound selector (.ajax) is now customizable via naja.uiHandler.selector.
    • To prevent people from shooting themselves in the foot, UIHandler now checks that the link target matches a set of allowed origins, and does not dispatch the asynchronous request if it does not match. You can configure the allowed origins by pushing them to naja.uiHandler.allowedOrigins.
    • Browser history manipulation can be disabled for specific requests by adding history: false to the request options, or data-naja-history="off" to the .ajax element.
    • Also, you can now decide on the front-end side whether you want to push a new state, or replace the current one, by adding history: 'replace' to the options or data-naja-history="replace" to the element.
    • Tests now run in real browsers via SauceLabs, so that there's a better guarantee that IE 10+ is actually supported. 🎉

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com