一个库,可将声明式 Ajax 功能添加到您的网站中
This is a plugin that Eldarion uses for all of its AJAX work.
No more writing the same 20 line $.ajax blocks of Javascript over and over
again for each snippet of AJAX that you want to support. Easily extend support
on the server side code for this by adding a top-level attribute to the JSON you
are already returning called "html" that is the rendered content. Unlike a
backbone.js approach to building a web app, eldarion-ajax leverages server side
template rendering engines to render and return HTML fragments.
This project used to be called bootstrap-ajax but the connection with Twitter Bootstrap was tenuous at best so we thought it best to rename to eldarion-ajax.
There is a demo project at https://github.com/eldarion/eldarion-ajax-demo/.
jQuery is required for this library so make sure it is included somewhere on the
page prior to the inclusion of eldarion-ajax.min.js.
Copy js/eldarion-ajax.min.js to where you keep your web sites static
media and the include them in your HTML:
You can also just include it in your own bundle.
npm install eldarion-ajax --save
require('eldarion-ajax'); // do this in your main bundle file and you'll be all set
There are three supported actions:
a.clickBinding to the a tag's click event where the tag has the class ajax:
Done
In addition to the href attribute, you can add data-method="post" to
change the default action from an HTTP GET to an HTTP POST.
form.submitConvert any form to an AJAX form submission quite easily by adding ajax
to the form's class attribute:
...
When submitting this form the data in the form is serialized and sent to the
server at the url defined in action using the method that was
declared in the form tag.
a.cancelAny a tag that has a data-cancel-closest attribute defined will
trigger the "cancel" event handler. This simply removes from the DOM any elements
found using the selector defined in the data-cancel-closest attribute:
Cancel
These custom events allow you to customize eldarion-ajax behavior.
eldarion-ajax:begineldarion-ajax:successeldarion-ajax:erroreldarion-ajax:completeeldarion-ajax:modify-dataAll events are triggered on the element that is declared to be ajax. For example on an anchor:
the trigger would be fired on the `` element. This event, of course, bubbles up, but allows you to easily listen only for events on particular tags.
Every event also sends as its first parameter, the element itself, in case you were listening at a higher level in the chain, you still would have easy access to the relevant node.
eldarion-ajax:beginThis is the first event that fires and does so before any ajax activity starts. This allows you to setup a spinner, disable form buttons, etc. before the requests starts.
A single argument is sent with this event and is the jQuery object for the node:
$(document).on(`eldarion-ajax:begin`, function(evt, $el) {
$el.html(`Processing...`);
});
eldarion-ajax:successThis event is triggered if the request succeeds. Four arguments are passed with this event: the jQuery object; the data returned from the server; a string describing the status; and the jqXHR object:
$(document).on('eldarion-ajax:success', '[data-prepend-inner]', function(evt, $el, data, textStatus, jqXHR) {
var $node = $($el.data('prepend-inner'));
$node.data(data.html + $node.html());
});
eldarion-ajax:errorThis event is triggered if the request fails. Four arguments are also passed with this event: the jQuery object, the jqXHR object; a string describing the type of error that occurred; and an optional exception object. Possible values for the third argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, the fourth argument receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."
eldarion-ajax:completeThis event is triggered when the request finishes (after the above success and
error events are completed). This is triggered from the document rather than
the element in context as the handlers processing success messages could replace
the DOM element and therefore would prevent the event from reaching your
listener. The element is always passed as the first argument with this event
(even if it no longer exists in the DOM). In response to a successful request,
the arguments passed with this event are the same as those of the success
event: the element, data, textStatus, and the jqXHR object. For failed requests
the arguments are the same as those of the error event: the element, the jqXHR
object, textStatus, and errorThrown.
eldarion-ajax:modify-dataThis is triggered with jQuery's triggerHandler so it functions more like a
callback. If you listen for it, you have to listen on the same element that you
have wired up to send AJAX data on as the event doesn't bubble up. Also, it will
send the original data that it serialized as a parameter and if you want to
change the data at all, you must return new data from the function handling the
event. Otherwise, the original data will be used.
There are three data attributes looked for in the response JSON.
location - URL used for immediate redirectionhtml - content used when processing html Directivesfragments - additional content for the DOMdata.locationIf location is found in the response JSON payload, it is expected to be a URL
and the browser is immediately redirected to that location. No additional HTML
processing is performed.
data.htmlThe data.html response element is typically used for insertion or replacement
of existing DOM element content. Exactly how data.html is used depends on one
or more processing directives.
Processing directives are defined by attributes added to the event-handling class="ajax" element.
They are linked to handlers as described in Handlers: A Batteries-Included Framework.
Each processing directive is assigned a CSS selector. Since a CSS selector can be written to address multiple different blocks on the page at the same time, large segments of the DOM can be modified with each directive.
All processing directives are executed for each event and any number of directives may be combined.
Append response JSON data.html to the element(s) found in the specified CSS selector.
Done
Clear the .html attribute of each DOM element found in the specified CSS selector.
Done
Invoke data-clear functionality using jQuery's closest method to interpret the selector.
Prepend response JSON data.html to the element(s) found in the specified CSS selector.
Done
Define which elements get refreshed.
Matching elements are refreshed with the contents of the url defined in their data-refresh-url
attribute.
Done
In this example, .done-score will fetch (GET) JSON from the url defined
by data-refresh-url, then replace itself with the contents of response JSON data.html.
Invoke data-refresh functionality using jQuery's closest method to interpret the selector.
Remove DOM elements found in the specified CSS selector.
Done
Invoke data-remove functionality using jQuery's closest method to interpret the selector.
Replace the element(s) found in the specified CSS selector with response JSON data.html.
Done
Invoke data-replace functionality using jQuery's closest method to interpret the selector.
Invoke data-replace-inner functionality using jQuery's closest method to interpret the selector.
Similar to data-replace functionality, but replaces the element(s) .html attribute.
data.html Processing Directive ExampleThis example shows combined use of data-append, data-refresh, and data-remove attributes
as data.html processing directives.
Done
It is rare to use many processing directives combined like this. Usually just one or two suffice.
data.fragmentsResponse JSON data.fragments should contain a list of key/value
pairs where keys are the selectors to content that will be replaced, and
values are the server-side rendered HTML content replacing elements
that match the selection.
data.append-fragmentsSimilar to data.fragments. Each fragment value is appended to the element(s) found in the key CSS selector.
data.inner-fragmentsSimilar to data.fragments. Each fragment value replaces the .html attribute for the element(s) found in the key CSS selector.
data.prepend-fragmentsSimilar to data.fragments. Each fragment value is prepended to the element(s) found in the key CSS selector
Both data.html and data.fragments are processed in a single response.
This gives you the ability to replace a submitted form with data.html content
while also updating multiple fragments of content on the page.
The following attributes are used by eldarion-ajax when processing supported Actions.
Used on an `` anchor tag, triggers the cancel event handler, which removes
from the DOM any elements found in the specified CSS selector.
Cancel
Defines the request method, i.e. "POST". If this attribute is not provided, the request method defaults to "GET".
Specify a URL which will return HTML content for the element.
The eldarion
暂无开放 Issues,或尚未同步最近议题。