- Passer au contenu principal
- Passer à « À propos de ce site »

# Data Ajax

Questions ou commentaires?

Needs translation

## Purpose

A basic AjaxLoader wrapper that inserts AJAXed-in content.

## Executing WET plugin on AJAXed-in content

    /**
     * @title Execute any WET plugin on AJAXed-in content
     * @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
     * @author @duboisp (Github)
     * @version 1.0.0+2016-10-14
     */
    (function( $, document, wb ) {
    	"use strict";
    	$('#mycontainer').on( "wb-contentupdated", function( event, data ){
    		// "data.ajax-type" contains the insersion method [after, append, before, prepend, replace]
    		// "data.content" contains the
    		var $elm = $(event.currentTarget);
    		$elm
    			.find( wb.allSelectors )
    				.addClass( "wb-init" )
    				.filter( ":not(#" + $elm.attr( "id" ) + " .wb-init .wb-init)" )
    					.trigger( "timerpoke.wb" );
    		/*
    		 * Since we are working with events we want to ensure that we are being
    		 * passive about our control, so returning true allows for events to always
    		 * continue
    		 */
    		return true;
    	});
    })( jQuery, document, wb );

Be careful to not execute it on every data-ajax container to avoid a possible recursive self calling situation.

## Working example

- English example
- French example

## How to implement

1. Add one of the following data-ajax attributes to an element, with the attribute value being the URL of the content to AJAX in:

   - `data-ajax-after`: Insert content after the element

         <section data-ajax-after="ajax/data-ajax-extra-en.html">
         	...
         </section>

   - `data-ajax-append`: Insert content at the end of the element

         <section data-ajax-append="ajax/data-ajax-extra-en.html">
         	...
         </section>

   - `data-ajax-before`: Insert content before the element

         <section data-ajax-before="ajax/data-ajax-extra-en.html">
         	...
         </section>

   - `data-ajax-prepend`: Insert content at the start of the element

         <section data-ajax-prepend="ajax/data-ajax-extra-en.html">
         	...
         </section>

   - `data-ajax-replace`: Replace the element with the content

         <section data-ajax-replace="ajax/data-ajax-extra-en.html">
         	...
         </section>

2. Optional (v4.0.12+): Filter the content using the URL hash (`ajax/data-ajax-filter-en.html#filter-id`) or a selector (`ajax/data-ajax-filter-en.html .filter-selector`).

## Cache busting

Before to use the cache busting mechanism with your data json instance, it's highly recommended to configure your server properly instead.

Various strategy can be set on the server side and those are communicated to the browser through an http header as defined in section 5 of RFC7234.

## Configuration options

Option | Description | How to configure | Values
--- | --- | --- | ---
Overlay type | Configure the origin and destination of the content to AJAX in. | Add the configuration attribute to the affected element with the value being the URL of the content to AJAX in. | `data-ajax-after`: Insert the content after the element. `data-ajax-append`: Insert the content at the end of the element. `data-ajax-before`: Insert the content before the element. `data-ajax-prepend`: Insert the content at the start of the element. `data-ajax-replace`: Replace the element with the content.
`url` | Configure the origin of the content to AJAX in. | `data-wb-ajax='{ "url": "location/of/ajax/fragment/file.html", "type": "replace" }'` | Default The value being the URL of the content to AJAX in. Array To be used in conjunction with `httpref` configuration. Array length must match the array length of `httpref`.
`type` | Configure how the destination of the content will be inserted. | `data-wb-ajax='{ "url": "location/of/ajax/fragment/file.html", "type": "replace" }'` | Be one of the valid value listed. `after` Insert content after the element `append` Insert content at the end of the element `before` Insert content before the element `replace` Replace content inside the element `prepend` Insert content at the start of the element
`nocache` | Prevent caching. Prior using the functionality, use the various caching strategies that can be set and communicated through http header from your server, as defined in section 5 of RFC7234. Also, please note that some server may not like to have an query appended to his url and you may get an HTTP error like "400 Bad Request" or "404 Not Found". Like a page served by a domino server will return 404 error if the query string do not start with "`?open`", "`?openDocument`" or "`?readForm`". | `data-wb-ajax='{ "url": "location/of/ajax/fragment/file.html", "type": "replace", "nocache": true }'` or `data-wb-ajax='{ "url": "location/of/ajax/fragment/file.html", "type": "replace", "nocache": "nocache" }'` | Default The browser will manage the cache based on how the server has sent the file. `true` Boolean, Use the same cache buster id for the user session. Clossing and opening the tab should generate a new cache busting id. `nocache` String, A new id is generated everytime the file is fetched
`nocachekey` | Prevent caching. Optional, it defined what query parameter name to use for the cache busting. | `data-wb-ajax='{ "url": "location/of/ajax/fragment/file.html", "type": "replace", "nocache": true, "nocachekey": "wbCacheBust" }'` | Default Will use "`wbCacheBust`" for the parameter name. String URL pre-encoded string
`httpref` | AJAX-in content on the condition that HTTP Referrer matches `httpref` value(s). Optional. | `data-wb-ajax='{ "url": "ajax/httpref.html", "type": "replace", "httpref": "data-ajax-en.html" }'` | Default No impact. String HTTP Referrer URL or a regular expression matching the referrer URL Array Array of strings containing target HTTP Referrer URLs

## Using httpref

You can use multiple `httpref` using an array to action a single `url` call.

    	<div data-wb-ajax='{
    						"url": "ajx-fragment.html",
    						"type": "replace",
    						"httpref": [
    							"temp/index.html",
    							"temp/example.html",
    							"lorem/ipsum/index.html",
    							"test.html"
    						]
    						}'>
    		<p>Default content</p>
    	</div>

You can use multiple `httpref` using an array with a matching length array of `url` values. Each `httpref` value will be checked until true and action the `url` value that has the matching array index.

    	<div data-wb-ajax='{
    						"url": [
    							"ajx-content1-en.html",
    							"ajx-content2-en.html",
    							"ajx-example-en.html",
    							"ajx-questionnaire-en.html"
    						],
    						"type": "replace",
    						"httpref": [
    							"temp/index.html",
    							"temp/example.html",
    							"lorem/ipsum/index.html",
    							"test.html"
    						],
    						}'>
    		<p>Default content</p>
    	</div>

## Events

Event | Trigger | What it does
--- | --- | ---
`wb-init.wb-data-ajax` | Triggered manually (e.g., `$( "[data-ajax-after], [data-ajax-append], [data-ajax-before], [data-ajax-prepend], [data-ajax-replace]" ).trigger( "wb-init.wb-data-ajax" );`). | Used to manually initialize the Data Ajax plugin. Note: The Data Ajax plugin will be initialized automatically unless the required markup is added after the page has already loaded.
`wb-ready.wb-data-ajax` (v4.0.5+) | Triggered automatically after the content has been AJAXed in. | Used to identify where content has been AJAXed in by the plugin (target of the event) and to pass along how the content was included ("after", "append", "before", "prepend" or "replace").
    $( document ).on( "wb-ready.wb-data-ajax", "[data-ajax-after], [data-ajax-append], [data-ajax-before], [data-ajax-prepend], [data-ajax-replace]", function( event, ajaxType ) {
    });
    $( "[data-ajax-after], [data-ajax-append], [data-ajax-before], [data-ajax-prepend], [data-ajax-replace]" ).on( "wb-ready.wb-data-ajax", function( event, ajaxType ) {
    });
`wb-ready.wb` (v4.0.5+) | Triggered automatically when WET has finished loading and executing. | Used to identify when all WET plugins and polyfills have finished loading and executing.
    $( document ).on( "wb-ready.wb", function( event ) {
    });
`wb-contentupdated` | Triggered automatically when data-ajax has finished to load the response. | Use to perform a secondary action upon ajax content is loaded
    $('#mycontainer').on( "wb-contentupdated", function( event, data ){
    });

## Source code

Data Ajax plugin source code on GitHub

Date de modification :
  2017-06-07