emc/xmlhttprequest-bundle

此包提供了一种统一的方式来处理和控制AJAX请求。更多信息请访问 https://github.com/chafiq/EMCXmlHttpRequest/blob/master/README.md

安装: 195

依赖者: 0

建议者: 0

安全性: 0

星标: 2

关注者: 0

分支: 1

开放问题: 1

类型:symfony-bundle

3.0 2015-04-01 21:13 UTC

This package is not auto-updated.

Last update: 2024-09-22 04:02:38 UTC


README

此包提供了一种统一的方式来处理和控制AJAX请求。

安装

  1. 下载 EMCXmlHttpRequest
  2. 配置自动加载器
  3. 启用包

步骤 1: 下载 EMCXmlHttpRequest

最终,EMCXmlHttpRequest 文件应下载到 vendor/bundles/EMC/Bundle/XmlHttpRequest 目录。

这可以通过几种方式完成,具体取决于您的偏好。第一种方法是标准的 Symfony2 方法。

使用 Composer

在您的 composer.json 中添加 EMCXmlHttpRequest

{
    "require": {
        "emc/xmlhttprequest-bundle": "*"
    }
}

现在,通过运行以下命令告诉 Composer 下载包

$ php composer.phar update emc/xmlhttprequest-bundle

使用子模块

如果您更喜欢使用 git 子模块,请运行以下命令

$ git submodule add git://github.com/chafiq/EMCXmlHttpRequest.git vendor/emc/xmlhttprequest-bundle/EMC/XmlHttpRequestBundle/
$ git submodule update --init

请注意,使用子模块需要在自动加载器中手动注册 EMC 命名空间

<?php
// app/autoload.php

$loader->registerNamespaces(array(
    // ...
    'EMC' => __DIR__.'/../vendor/bundles',
));

步骤 2: 启用包

最后,在内核中启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new EMC\Bundle\XmlHttpRequestBundle\EMCXmlHttpRequestBundle(),
    );
}

依赖项

示例

简单 POST

控制器代码

    use EMC\XmlHttpRequestBundle\Annotation\XmlHttpRequest;

    /**
     * @Route("/ajax/", name="_ajax_call")
     * @XmlHttpRequest(type="json") // You can add streaming=true to get the streaming mode
     */
    public function ajaxAction()
    {
    	/**
    	 * Data result
    	 * mixed
    	 * The response will be "{code:0,data:{1:'My result',2:'My fancy bundle',5:'My reponse'}}"
    	 */
        return array(
        	1 => 'My result',
        	2 => 'My fancy bundle',
        	5 => 'My reponse'
        );
    }

模板代码

...
	<script type="text/javascript">
		$(document).ready(function(){
		
			var params = {test : 123};
			
			/* "xml_http_request" is a twig extension.
			   It creates the JavaScript call using the route annotation "XmlHttpRequest".
			   The first and second parameters are used for creating the route path.
			   The third parameter is the name of data source parameters for the ajax call.
			   You can add a fourth parameter which is the name of the JS callback.
			   
			   @see @EMCXmlHttpRequestBundle\Twig\XmlHttpRequestExtension for more information
			   
			   The generated code of "{% xml_http_request('_ajax_call', {}, 'params') %}" is :
			   
			   EMCXmlHttpRequest.getInstance().stream("http://www.dev.local/app_dev.php/demo/ajax/", params, "json", null);
			*/
			var result = {% xml_http_request('_ajax_call', {}, 'params') %}
			
			console.log( result );
		});
	</script>
...

流式信息

控制器代码

    use EMC\XmlHttpRequestBundle\Annotation\XmlHttpRequest;
    use EMC\XmlHttpRequestBundle\Event\StreamingProgress;

    /**
     * @Route("/ajax/", name="_ajax_call")
     * @XmlHttpRequest(type="json", streaming=true)
     */
    public function ajaxAction()
    {
    
    	for($i=0; $i<5; $i++) {
    		
    		$event = new StreamingProgress($i*20, 'Execution message info ' . ($i*20) . '% ...');
    		
    		$this->get('event_dispatcher')->dispatch( 'emc.streaming.progress', $event);
    		
    		sleep(1);
    	}
    	
        return array(
        	1 => 'My result',
        	2 => 'My fancy bundle',
        	5 => 'My reponse'
        );
    }

模板代码

与第一个示例相同。