pheryjs / phery

This library unleashes everything you expect from an AJAX library, AJAX file upload, json, nested AJAX responses, merging and unmerging responses, direct access to the DOM element that is making the AJAX call. Plays well with libraries and frameworks such as AngularJS, Ember, Knockout and Backbone bridging to PHP
http://phery-php-ajax.net/
MIT License
135 stars 36 forks source link

Phery::set() specification incomplete, unclear #46

Open Chealer opened 8 years ago

Chealer commented 8 years ago

The documentation for Phery::set() reads:

set( array $functions )

Sets the functions to respond to the ajax call. For security reasons, these functions should not be reacheable through POST/GET requests. These will be set only for AJAX requests as it will only be set in case of an ajax request, to save resources.

You may set the config option "set_always_available" to true to always register the functions regardless of if it's an AJAX function or not going on.

The answer/process function, should have the following structure:

function func($ajax_data, $callback_data, $phery){
  $r = new PheryResponse; // or PheryResponse::factory();

  // Sometimes the $callback_data will have an item called 'submit_id',
  // is the ID of the calling DOM element.
  // if (isset($callback_data['submit_id'])) {  }
  // $phery will be the current phery instance that called this callback

  $r->jquery('#id')->animate(...);
  return $r; //Should always return the PheryResponse unless you are dealing with plain text
}

Parameters

$functions array $functions An array of functions to register to the instance.

array(
  'function1' => 'function',
  'function2' => array($this, 'method'),
  'function3' => 'StaticClass::name',
  'function4' => array(new ClassName, 'method'),
  'function5' => function($data){}
);

Returns Phery

Which ajax call this refers to is unclear. The description of $functions suggests the elements should be functions, but that is not always the case, as seen in the first 3 elements. This also fails to specify that the array must be string-indexed. This is mentioned in the README's take on that function, which uses the synopsis "Phery::instance()->set(array $functions)":

Register the functions that will be triggered by AJAX calls. The key is the function alias, the value is the function itself.

<?php
    function outside($ajax_data, $callback_data){
        return PheryResponse::factory();
    }

    class classy {
        function inside($ajax_data, $callback_data){
            return PheryResponse::factory();
        }

        static function inside_static($ajax_data, $callback_data){
            return PheryResponse::factory();
        }
    }

    $class = new classy();

    Phery::instance()->set(array(
        'alias' => function(){
            return PheryResponse::factory();
        },
        'outside' => 'outside',
        'class' => array($class, 'inside'),
        'class' => 'classy::inside_static',
        'namespaced' => 'namespaced\function'
    ));
?>

Callback/response function comprises of:

<?php
    function func($ajax_data, $callback_data, $phery_instance){
        // $ajax_data = data coming from browser, via AJAX
        //
        // $callback_data = can have anything you specify, plus additional information, like **submit_id** that
        // comes automatically from the AJAX request, containing the ID of the calling DOM element, if has an id="" set
        //
        // $phery_instance = the current instance of Phery
        //
        return PheryResponse::factory(); // In most cases, you'll want to return a PheryResponse object
    }
?>

I do not know what the last sentence was meant to read, but it is invalid as it stands. Either a whole comprises parts, or a whole is comprised of parts. x cannot "comprise of" y. French has 2 distinct verbs for that ("est constitué de" vs "comprend").

pocesar commented 8 years ago

Yes, the AJAX function (the one specified on the client side) is the key of the array. The callback is the value of the array. There are many ways you can call an user function in PHP, I tried to show what is possible that will work with the set function.

Sorry about the english part, my native language is brazilian portuguese, sometimes I find it hard to find equivalents in english.

Chealer commented 8 years ago

Yes, the examples are very useful but this method is of high importance and should be well specified too. I recommend replacing the term "function" with either "callback" or "callable" (used in the PHP manual).

You have nothing to be sorry about; English is non-trivial, and only a few are lucky enough to speak it natively.