JS-scripts

For operation of the module often requires a separate JavaScript-script. There are two ways to connect:

Automatically connect

File modules/module_name/js/module_name.js automatically connected to the module with the attached page. File modules/module_name/js/module_name.template_name.js connects automatically to the module template.

Для работы модуля часто нужен отдельный JavaScript-сценарий. Есть два пути их подключения:

Example:

By template modules/shop/views/shop.view.buy_form.php automatically load the file modules/shop/js/shop.buy_form.js.

File modules/shop/js/shop.js to load all the pages of the module (if the module is connected to the page of the site), but do not load a module to the template tags.

Files are not duplicated. For example, the file modules/shop/js/shop.buy_form.js connect once, even if the "Buy" button several times will be displayed.

Manual connection

You can specify directly the script that you want to connect. This can be done in any location in the pattern or model in the module.

Example:

// internal link
$this->diafan->_site->js_view[] = 'modules/search/js/search.show_search.js';
// external link
$this->diafan->_site->js_view[] = 'http://www.google.com/recaptcha/api/js/recaptcha_ajax.js';

All modules JS-files is going to generate the page and output by template tag show_js. All local files are compressed, some files are loaded asynchronously. It is important that the template tag <insert name="show_js"> connected JS-files was end HTML-document. Before closing tag </body>.

Standard Ajax-requests processing

In DIAFAN.CMS provides data sending form Ajax-request, if the form is an attribute class="ajax". The answer received data in JSON format, which is always the same processed. Standard process the following data (specified in the file modules/module_name/module_name.action.php).

Example:

// will redirect to home
$this->result["redirect"] = '/';

// messages are inserted in special fields
// general message add to container <div class="error"></div> usually under the button <input type="submit">
$this->result["errors"][0] = 'The general message to all fields';
// message to a particular field be added to the container <div class="error_name"></div>
$this->result["errors"]["name"] = 'Enter the name';

$this->result["data"] = array(
    
// "first data" framed in the block <div class="target"></div>
    
".taget" => "first data",

    
// "second data" framed in the block <div id="target2"></div>
    
"#target2" => "second data",

    
// "text instead of form" replace form
    
"form" => "text instead of form",

    
// block <div class="target3"></div> will be hide
    
".target3" => false,
);

// refresh captcha
$this->result["captcha"] = 'captcha HTML-code';

// clear form
$this->result["result"] = "success";

// add data to the end of the block `<div class="value_of_attribute_id_of_form"></div>`
$this->result["add"] = "data";

// load uploaded files
$this->result["attachments"] = "block uploaded files";

// load uploaded images
$this->result["images"] = "block uploded images";

// update ID hash user
$this->result["hash"] = "new hash";

The module JS-file, you can finish your treatment for the action before the form is submitted and the results obtained. These actions complement the standard treatment if the function returns true or replace it if the function returns false. Format:

diafan_ajax.before['form_label'] = function(form){}

diafan_ajax.success['form_label'] = function(form, response){}

Form label consists of a module name and the form action. That is, the contents of the form fields:

<input name="module">
<input name="action">

Example:

// before sending the ads search form ads function looks at whether there is a container <div class="ab_list"></div>
// if so, what data is sent to the Ajax-request
diafan_ajax.before['ab_search'] = function(form){
    if(! $(
".ab_list").length)
    {
        $(
form).removeClass('ajax').submit();
        return
false;
    }
    $(
form).attr('method', 'POST');
}

// when the answer came, the data is placed in a container <div class="ab_list"></div>
// and canceled standard treatment
diafan_ajax.success['ab_search'] = function(form, response){
    var
k = 0;
    $(
".ab_list").text('');
    $(
".ab_list").first().html(response.data).focus();
    return
false;
}