Yet Another Ajax Helper (pronounced Yaah! or just Ya!)
YAAH helps to easily handle:
- simple ajax requests
- infinite load more
- content lazy loader
- url rewrite on ajax and history (>=IE 10)
Needs jQuery, Modernizr
class="yaah-js": Bind the event to the desired element
data-ya-href: Link to the content to insert (can be href if the element is an anchor)
always: Always trigger the request (default)once: Trigger the request one time onlychange: Trigger the request only when change event is triggered (for<input />)submit: Submit Form in AJAX with serialized dataautoload: Autoload the request on DOMReadymanual: Triggered manually when bind the associated event
replaceReplace the target elementafter: Insert after the target elementbefore: Insert before the target elementinner: Replace the content inside the target elementtop: Insert at the beginning of the target elementbottom: Insert at the end of the target elementremove: Remove the target elementnone: Nothing is replaced or injected
#CSS .Selector: Can be whatever CSS Selector you like but most of the time you'll want it unique. If empty, clicked element.
http://path/to/url: Redirect after AJAX to the url (page reload)
true: Stop event propagation on click. Useful if you don't want to trigger parent events.
JavaScript! Do you speak it ?: Display the message in a confirm box, trigger AJAX on confirm only.
url/to/push: url to push to the history
GET,POST, etc.: the form method to use for the AJAX Request. If not specified,GETis used as default, andPOSTif there isdata-ya-post.
serializedData: String of serialized data to pass to the AJAX request
time in seconds: Number of seconds between AJAX call
Events are triggered to the element with the yaah-js class.
Most of them are similar to regular XHR events:
yaah-js_xhr_submit: event triggered on form submit just before data fetchyaah-js_xhr_manualTrigger: the event to trigger for "manual" triggeryaah-js_xhr_beforeSend: before the XHR is sentyaah-js_xhr_complete: XHR completeyaah-js_xhr_beforeInsert: XHR success, but before updating target with datayaah-js_xhr_success: XHR success, after target updated with data (please read below if.yaah-jselement is in target)
For case where your .yaah-js element is in the target (and then, removed when data is updated), you might not be able to get the event catchable on any element with delegation, like that :
$(document).on('yaah-js_xhr_success', '.yaah-js', …);
So, for this specific behavior, YAAH generates an unique event id sent in yaah-js_xhr_beforeInsert event, then triggered after the data update.
Use it only if your element is in the target. Otherwise, you can juste use the yaah-js_xhr_success event
$(document.body).on('yaah-js_xhr_beforeInsert', '.yaah-js', function(e, eventId, target, item, data) {
$(document).one(eventId, function(e, target, item, data){
// do whatever you want in the target, with the target, or with the data.
});
});- Touch support
- Scroll