manuelj555/ajax-bundle

Symfony AjaxBundle

安装次数: 12,814

依赖: 0

推荐者: 0

安全性: 0

星星: 1

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v2.0.3 2019-05-20 16:04 UTC

This package is auto-updated.

Last update: 2024-09-21 03:28:31 UTC


README

此Bundle允许在任何操作(表单、重定向、关闭模态框、触发JavaScript事件、处理错误和表单错误、显示闪存消息等)中处理Ajax请求。需要jQuery。

安装

添加到composer.json

{
  "require": {
    "manuelj555/ajax-bundle": "2.0.*@dev"
  }
}

执行composer update。

配置

注册Bundle

<?php
// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Ku\AjaxBundle\KuAjaxBundle(),
        // ...
    );
}

Ajax处理器

使用示例

控制器

public function createAction(Request $request)
{
    $user = new User();
    $form = $this->createForm(new UserType(), $user, array(
        'action' => $request->getRequestUri(),
    ));
    $form->handleRequest($request);

    if ($form->isSubmitted() and $form->isValid()) {
        $this->get('fos_user.user_manager')
          ->updateUser($user);

        $this->addFlash('success', 'User Created!');

        $this->get('ku_ajax.handler')->success();
        // Calling to succes method on ajax Handler, stop the redirection on ajax request 
        // and send a status code 200
        return $this->redirectToRoute('admin_company_users_list', array('companyId' => $company->getId()));
    }elseif($form->isSubmitted()){
      // invalid form
      $this->get('ku_ajax.handler')->badRequest();
      // this send a status code 400
    }

    return $this->render('user/create.html.twig', array(
        'form' => $form->createView(),
    ));
}

视图

$('#user-form').on('submit', 'form', function (e) {
  e.preventDefault();
  var $form = $(this);
  $.post(this.action, $form.serialize(), function () {
    // this callback is called on success response
    $("#myModal").modal('hide');
  }).fail(function (xhr) { //this method is called on ajax error
    if(xhr.status == 400){
     // invalid form data
      $form.replaceWith($(html).find('form'));
    }
  });
});

Ajax处理器方法

$this->get('ku_ajax.handler')->success($statusCode = 200);
$this->get('ku_ajax.handler')->error($message, $statusCode = 400)
$this->get('ku_ajax.handler')->badRequest($statusCode = 400)
$this->get('ku_ajax.handler')->redirect($isOk = true, $statusCode = 278)

闪存消息

此Bundle允许通过JavaScript处理ajax请求中的闪存消息。需要jQuery。

在config.yml(所有配置均为可选)中

ku_ajax:
    handler: ~
    flash_messages:
        auto_assets:
            pnotify: ~
    #        sticky: ~
        mapping:
    #        success:
    #            title: Información
    #            icon: my-icon
    #        info:
    #            title: Información

flash_messages: auto_assets

自动将JavaScript和CSS添加到HTML内容中。您可以选择要使用的插件,可用的选项有

flash_messages: mapping

允许为每个设置的映射类型设置标题、图标和类型,以便在JavaScript中使用。

示例

ku_ajax:
    flash_messages:
        mapping:
            success:
                 type: success
                 title: Información
                 icon: my-icon
             info:
                 type: info
                 title: Información
             error:
                 type: danger
                 title: Error

手动资产安装

如果您未启用auto_assets配置,您可以使用位于Bundle中的twig视图

  • KuAjaxBundle:flash:pnotify.html.twig 或
  • KuAjaxBundle:flash:sticky.html.twig

使用示例

{% use 'KuAjaxBundle:flash:pnotify.html.twig' %}
{#{% use 'KuAjaxBundle:flash:sticky.html.twig' %}#}
<!DOCTYPE html>
<html>
    <head>
        ...
        
        {{ block('ajax_flash_css') }}
    </head>
    <body>
        ...
        
        {{ block('ajax_flash_js') }}
        {{ block('ajax_flash_plugin') }}
    </body>
</html>

JavaScript插件

用法

$.ajaxFlash('*', function (message, type, title, icon) {
    //call on all flash types. this function is called for each flash message
    //the message parameter is a string
});
$.ajaxFlash('success info', function (message, type, title, icon) {
    //call on success and info flash types. this function is called for each flash message
    //the message parameter is a string
});
$.ajaxFlash('error', function (message, type, title, icon) {
    //call on error flash type. this function is called for each flash message
    //the message parameter is a string
});

// Working with array messages:

$.ajaxFlash(function (messages, type, title, icon) {
    //call in all flash types, this function is called one time for each message type.
    //the messages parameter is an array.
});

$.ajaxFlash(function (messages, type, title, icon) {
    //call success and info flash types, this function is called one time for each message type.
    //the messages parameter is an array.
}, 'success info');