jQuery自定义事件不会在AJAX成功回调中触发(jQuery Custom Event not triggering in AJAX Success Callback)
我正在试验我的应用程序中的jQuery
custom Events
。在下面的jQuery AJAX请求代码中,在
success
回调中,我试图trigger()
我的自定义Event
它似乎并没有触发它。 这会不会在成功回调中完成,需要在其他地方调用?
jQuery AJAX请求代码
// Request Task Record using AJAX Request to Server var jqXHR = $.ajax({ type: 'POST', async: false, contentType: 'application/json; charset=utf-8', dataType: 'json', url: projectTaskModal.cache.getTaskRecordUrlEndpoint, data: { action: 'load-task-record', task_id: taskId }, success: function(data) { // NON RELEVANT CODE REMOVED FOR DEMO // Event that is not being triggered.... // Publish 'task-modal-data-loaded' Event to Subscribers $.event.trigger({ type: 'task-modal-data-loaded', source: 'ajax', message: 'Task Modal Object Loaded from AJAX Request', time: new Date() }); } }); // end AJAX request
自定义事件订阅者
$(document).on('task-modal-data-loaded', function(event) { //alert('Task Modal Object Loaded'); console.log('CUSTOM APP EVENTS: Event: task-modal-data-loaded Source: '+event.source+' Message: '+event.message+' Time: '+event.time); });
I am experimenting with jQuery
custom Events
in my Application.In the jQuery AJAX request code below, in the
success
callback I am trying totrigger()
my customEvent
It does not seem to be triggering it though. Will this simply not be able to be done in the success callback and needs to be called elsewhere?
jQuery AJAX request code
// Request Task Record using AJAX Request to Server var jqXHR = $.ajax({ type: 'POST', async: false, contentType: 'application/json; charset=utf-8', dataType: 'json', url: projectTaskModal.cache.getTaskRecordUrlEndpoint, data: { action: 'load-task-record', task_id: taskId }, success: function(data) { // NON RELEVANT CODE REMOVED FOR DEMO // Event that is not being triggered.... // Publish 'task-modal-data-loaded' Event to Subscribers $.event.trigger({ type: 'task-modal-data-loaded', source: 'ajax', message: 'Task Modal Object Loaded from AJAX Request', time: new Date() }); } }); // end AJAX request
Custom Event Subscriber
$(document).on('task-modal-data-loaded', function(event) { //alert('Task Modal Object Loaded'); console.log('CUSTOM APP EVENTS: Event: task-modal-data-loaded Source: '+event.source+' Message: '+event.message+' Time: '+event.time); });
原文:https://stackoverflow.com/questions/29933792
最满意答案
无法为Application \ Controller \ AliasSegment获取或创建实例
如果这是控制器,那么我希望在module.config.php中有:
'controllers' => array( 'invokables' => array( '\Application\Controller\AliasSegment' => '\Application\Controller\AliasSegment', ) ),
你的类的命名空间看起来有点奇怪:
命名空间Zend \ Mvc \ Router \ Http;
关于什么:
namespace Application\Controller;
OK, I've made it. The important thing for this Thread: ZF2: How to get Zend\Navigation inside custom route?.
You can use any segment type route. But this may need a little modifications to
match
function.If navigation's single page will have 'link' param, the url will be converted to 'link' string but other params will stay behind it. Just think of it as an overlay for default URI of current route.
I had to modify my custom route class a little bit. First of all, i had to change its namespace to
Application\Router
. Here is a full class:// EDIT - file within ModuleName/src/Router/Alias.php namespace Application\Router; use Traversable; use Zend\Mvc\Router\Exception; use Zend\Stdlib\ArrayUtils; use Zend\Stdlib\RequestInterface as Request; use Zend\Mvc\Router\Http; class Alias extends Http\Segment { private static $_navigation = null; public function match(Request $request, $pathOffset = null) { $uri = $request->getUri(); $path = $uri->getPath(); $items = self::$_navigation->findAllBy('route', 'node'); $params = null; if($items != null){ $t = sizeof($items); for ($i=0; $i < $t; $i++) { $item = $items[$i]; $params = $item->getParams(); if (isset($params['link']) && $params['link']==$path){ $uri->setPath('/'.$item->getRoute().'/'.$params['id']); $request->setUri($uri); break; } } } return parent::match($request, $pathOffset); } public function setNavigation($navigation){ self::$_navigation = $navigation; } protected function buildPath(array $parts, array $mergedParams, $isOptional, $hasChild) { if(isset($mergedParams['link'])){ return $mergedParams['link']; } return parent::buildPath($parts, $mergedParams, $isOptional, $hasChild); } }
here is sample part of
module.config.php
:'navigation' => array( // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key 'default' => array( // And finally, here is where we define our page hierarchy 'account' => array( 'label' => 'Account', 'route' => 'node', 'params' => array( 'id' => '2', ), 'pages' => array( 'home' => array( 'label' => 'Dashboard', 'route' => 'node', 'params' => array( 'id' => '8', 'link' => '/about/gallery' ), ), 'login' => array( 'label' => 'Sign In', 'route' => 'node', 'params' => array( 'id' => '6', 'link' => '/signin' ), ), 'logout' => array( 'label' => 'Sign Out', 'route' => 'node', 'params' => array( 'id' => '3', ), ), ), ), ), ), 'router' => array( 'routes' => array( 'home' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Application\Controller\Index', 'action' => 'index', ), ), ), 'node' => array( 'type' => 'Application\Router\Alias', 'options' => array( 'route' => '/node[/:id]', 'constraints' => array( 'id' => '[0-9]+' ), 'defaults' => array( '__NAMESPACE__' => 'Application\Controller', 'controller' => 'Index', 'action' => 'index', 'id' => '0' ), ), 'may_terminate' => true, ), ), ),
相关问答
更多-
ZF2 - 从路线生成Url(ZF2 - Generating Url from route)[2022-07-25]
在调用$this->url(...)之前使用echo(见下文),这将显示整个URL。 url('route-name', $urlParams, $urlOptions); ?> 请注意, url()的第一个参数是在[module]/config/module.config.php文件中指定的路由名称。 有关ZF2的URL视图助手的更多信息,请参阅此处。 编辑回应问题编辑: 以上部分与使用URL视图助手相关。 如果你想在控制器中有一个URL,那么你需要URL控制器插件。 ... -
请求可以由多个路由匹配。 一个例子:url / foo可以通过文字路由/foo和路由[/:action]匹配。 要区分这两条路线, 配置它们的顺序很重要 。 会发生什么,路由以LIFO顺序匹配,因此最后一条路线必须是最明确的 。 在“/ foo”示例中,此配置将与文字匹配: 'router' => array( 'routes' => array( 'test1' => array( 'type' => 'segment', 'o ...
-
使用Apache的ZF2:在远程浏览器上路由到我的模块时出现问题(ZF2 with Apache: problems routing to my module on remote browser)[2023-03-17]
问题在于你的apache vhost配置。 您正在提供错误的目录( /var/www/html/myproject )。 您应该提供公用文件夹而不是根项目文件夹。 您现在必须像这样请求用户页面: http://192.168.0.180/myapp/public/user 哪个错了,网址应该只是: http://192.168.0.180/user 更改您的vhost配置,以便像这样提供公共目录: /var/www/html/myproject/myapp/public The problem lie ... -
路由配置是这里唯一重要的部分。 目前,您有/overview的路由,其中包含/cmstool的子路由。 这将匹配以下URL: /overview /overview/cmstool 不完全是你追求的。 您可以通过几种不同的方式对其进行配置。 最接近您目前所拥有的那条路线将是/affiliates的路线,具有两条儿童路线,每条路线用于您的每个行动。 这个配置是: 'router' => array( 'routes' => array( 'affiliates' => array( ...
-
无法为Application \ Controller \ AliasSegment获取或创建实例 如果这是控制器,那么我希望在module.config.php中有: 'controllers' => array( 'invokables' => array( '\Application\Controller\AliasSegment' => '\Application\Controller\AliasSegment', ) ), 你的类的命名空间看起来有点奇怪: 命名空 ...
-
我迟到了,但它对新手来说仍然有用。 现在的解决方案是从\Zend\Test\PHPUnit\Controller\AbstractControllerTestCase继承,因此用法与ZF1非常相似: class IndexControllerTest extends \Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase { public function setUp() { $this->setApplica ...
-
url ('application/default', array ('controller' => 'xxx', 'action' => 'x')); ?> url ('application/default', array ('controller' => 'xxx', 'action' => 'x')); ?>
-
你应该使用urlencoded字符: 'route' => '/kundtj%C3%A4nst/', You should use urlencoded characters: 'route' => '/kundtj%C3%A4nst/',
-
zf2中的路由首字母冲突(Routing initials conflicts in zf2)[2022-04-30]
如果发生冲突,请优先尝试 具有高等级的优先级是路由器检查正确路由的最高优先级。 return array( 'router' => array( 'routes' => array( 'admin' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'priority' => 100, // <---- ... -
首先,我强烈建议您开始使用虚拟主机来摆脱烦人的localhost/bla/public 其次,您尝试访问此路线: yourdomain/spanel/test/index ,但没有迹象表明您在路线中的任何位置分配了部件/spanel ,您指定的全部是/test[...] 简而言之,您想要访问您的域yourdomain/test/index或者您想修改您的路由以包含/spanel部分 First of all i strongly suggest that you start working with vir ...