dm/ajaxcom

Controls Ajax from PHP

Installs: 13 946

Dependents: 0

Suggesters: 0

Security: 0

Stars: 15

Watchers: 20

Forks: 5

Open Issues: 7

Language:JavaScript

v1.6.2 2017-09-27 05:35 UTC

README

Build Status Scrutinizer Code Quality

AjaxCom

AjaxCom is a PHP library that allows developers to write their ajax code in PHP with minimal javascript.

Demo

http://ajaxcom.everlution.sk/

Features

  • Append html to elements
  • Prepend html to elements
  • Replace elements with new html
  • Set html of elements
  • Set value of elements
  • Display flash messages
  • Display modals
  • Change URL
  • Call functions

Requirements

  • PHP >= 5.3.3
  • jQuery >= 1.7.x

Installation

Via composer:

{
    "require": {
        "dm/ajaxcom": "dev-master"
    }
}

Usage

Javascript

Include the javascript library located at src/DM/AjaxCom/Resources/public/js/ajaxcom.js

Intercept all click events on anchors and submit events on forms:

$(document).ajaxcom();

Or just intercept those which have data-ajaxcom

$(document).ajaxcom('[data-ajaxcom]');

PHP

use DM\AjaxCom\Handler;

if (isset($_SERVER['X-AjaxCom'])) {
    // Render page using AjaxCom library
    $handler = new Handler();
    // Change URL to /newurl
    $handler->changeUrl('/newurl');
    // Append some html to an element
    $handler->container('#table')
        ->append('<tr><td>This is a new row</td></tr>');
    // Replace element with some new html
    $handler->container('#something')
        ->replaceWith('<span id="somethingnew">Some text</span>');
    // Display modal
    $handler->modal(
        '<div class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3 id="myModalLabel">Modal header</h3>
            </div>
            <div class="modal-body">
                <p>One fine body.</p>
            </div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
                <button class="btn btn-primary">Save changes</button>
            </div>
        </div>'
    );
    
    // NOTE: It is important to call callback() AFTER container() or modal()
    // when you are manipulating the rendered DOM inside the callback;
    // otherwise the callback will be called before elements of DOM are loaded
    
    // Call funcname()
    $handler->callback('funcname');
    // Call namespace.funcname()
    $handler->callback('namespace.funcname');
    // You can also specify parameters which will be passed as object to the funcion
    $handler->callback('namespace.funcname', ['this' => 'will', 'be' => 'passed', 'as' => 'an object', 'to' => 'the function']);
    

    header('Content-type: application/json');
    echo json_encode($handler->respond());
} else {
    // Render page normally
}