Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/troopers/ajaxbundle

This bundle offers a simple structure to run ajax actions
https://github.com/troopers/ajaxbundle

ajax jquery symfony symfony-bundle troopers

Last synced: 29 days ago
JSON representation

This bundle offers a simple structure to run ajax actions

Awesome Lists containing this project

README

        

[![Troopers](https://cloud.githubusercontent.com/assets/618536/18787530/83cf424e-81a3-11e6-8f66-cde3ec5fa82a.png)](http://troopers.agency/?utm_source=AjaxBundle&utm_medium=github&utm_campaign=OpenSource)

[![License](https://img.shields.io/packagist/l/troopers/ajax-bundle.svg)](https://packagist.org/packages/troopers/ajax-bundle)
[![Version](https://img.shields.io/packagist/v/troopers/ajax-bundle.svg)](https://packagist.org/packages/troopers/ajax-bundle)
[![Packagist DL](https://img.shields.io/packagist/dt/troopers/ajax-bundle.svg)](https://packagist.org/packages/troopers/ajax-bundle)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/0c7ec932-671e-4bd1-8ecd-3d3a6c015909/mini.png)](https://insight.sensiolabs.com/projects/0c7ec932-671e-4bd1-8ecd-3d3a6c015909)
[![Twitter Follow](https://img.shields.io/twitter/follow/troopersagency.svg?style=social&label=Follow%20Troopers)](https://twitter.com/troopersagency)
=============

TroopersAjaxBundle
============

## Description

This bundle offers a simple structure to run ajax actions.
For each kind of action (link or form), you have to add a data-toggle="ajax" to the tag ( **a**, **form** ) and to specify the id of the container to update in the update attribute.

## Installation

With Composer :

Add this line in your composer.json file :

"troopers/ajax-bundle": "dev-master"

Declare the bundle in your AppKernel.php:

public function registerBundles() {
$bundles = array(
[...]
new Troopers\AjaxBundle\TroopersAjaxBundle(),
[...]

## Configuration

### AsseticInjectorBundle way

If You have installed our [AsseticInjectorBundle](https://github.com/Troopers/AsseticInjectorBundle/blob/master/README.md) bundle:

1. Thank you, you are awesome for us ;)
2. It may "just work" but if not, you'll have to check the injector tags in your javascript (`injector="foot"`) and stylesheet (`injector="head"`) blocks.

### The `normal` way

Just load `ajax.js`and `ajax.css` in your template:

```html


...
{% block stylesheets %}

{% endblock %}


...
{% block javascripts %}


{% endblock %}

```

## Examples

### Links

Click me

Here will stand the ajaxCall response

### Forms



Here will stand the ajaxAction response

### Link that updates a form (using the data-form jquery selector)

Show more

If the link contains the data-form property, it will submit the form using the properties of the link.

### Auto-refresh a form

You can automatically send an ajax request to update your form when an input/select change, just add the data attribute "data-refreshonchange":

```html

Transport
Stucture

```

Then in your controller's action:

```php
if ($request->query->get('novalidate', false) === false) {
if ($form->isValid()) {
// form is valid
} else {
// avoid to display errors when novalidate
$form = $formFactory->createForm();
$form->setData($user);
}
}
```

For some reason, you would not refresh some parts of your form (for example an input type="file"). Then, add the data attribute 'data-ignoreonchange="$some_unique_id"'.

Extra features
---

As you probably noticed, you can automatically set a container for your ajax response. Actually, you can do some others features :

### Update strategy

The data-update-strategy attribute is used to define the function to use to integrate your ajax content with the container your already set by using the data-update attribute. So if you want to, you can tell the library to put after, before, append, prepend or a custom function. Note that, the default behavior is to simply replace the content of the container by the html function.

Let's take an example. If you want your ajax content to be simply added in the end of your container, here is your code :

Click me

This sentence will stay here and the ajax content will be displayed just after

### Effect

By default, when we load ajax content, a small effect is run : hide + fadeIn (if container not empty) hide + slideDown (if container is empty).
If this effect does not feed your needs, you can set your own by filling the data-effect attribute.
For example :

Click me

This container will be hidden, the ajax content will be placed here and then the slideDown function will be used to display this

If you do not want any effect, you can just add the data attribute noEffect on the link (or the target) tag.
For example, on the link :

Click me, no effect

This container will be hidden without any effect if you click

or directly on the target :

Click me, no effect
Click me (fadeIn)

This container will be hidden with or without an effect, according by the link you choose

In this last example, the first link'll do the ajax call without any effect because of the data-noEffect on the target container and the second, because of its override will trigger a fadeIn effect.

### Bootstrap Modal use

This is the is the youngest of the ajax feature's family, now you can simply trigger bootstrap modal by simply adding data-toogle="modal" on the link you decides to :

Click me and the ajax result will pop in a beautiful popup

This will work "as is" but to have a cool appearence, you will have to add the correct markup inside the modal. More info in the [Twitter Bootstrap modal doc](http://getbootstrap.com/2.3.2/javascript.html#modals)

### Overwrite the loader

`AjaxBundle` comes with a default loader and an overlay. If you want, you can change it by defining the loader's markup you want to use in `window.loader`

```html



{% block title %}Welcome!{% endblock %}
{% block stylesheets %}

{% endblock %}



{% block body %}{% endblock %}
{% block javascripts %}


window.loader = '<div id="canvasloader-container" style="display: none;"><img src="{{ asset('/img/loading.gif') }}" style="width: 80%; padding-top: 15px;"/></div>';
window.loaderOverlay = null;


{% endblock %}

```
This example will tell ajax.js to use the `/img/loading.gif` and will disable the overlay.