如何实现“深层链接”通过 ZF 进行 Ajax 调用?
我觉得需要让浏览器后退按钮正常工作,并允许用户为他们看到的内容添加书签。
我在 Zend 路线上并不多才多艺,但目前我无法改变这一点。
这是我正在使用的 ajax 实现方法:
class TestController extends Zend_Controller_Action {
public function init()
{
/* Initialize action controller here */
if ($this->getRequest()->isXMLHttpRequest()) {
$this->_helper->layout()->setLayout('blank');
$logger = $this->getInvokeArg('bootstrap')->getResource('Log');
$logger->debug('AJAX Call');
}
}
public function indexAction()
{
// render the default page
}
public function somethingelseAction()
{
// do something else render something.
}
}
让我的初始视图使用目标 div 以及一些链接进行渲染...这是我的 index.phtml:
<h1>Tests...</h1>
<a class="ajaxloader"
href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'speed'), null, true);?>">Speed</a>
<a class="ajaxloader"
href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'somethingelse'), null, true);?>">Something Else</a>
<div id="testresults">
<h1>Default stuff to show.</h1>
</div>
一些附加到这些“ajaxloader”链接和目标的 jQuery 代码结果到“testresults”div。
$(function() {
$('.ajaxloader').click(function(event) {
var target = $(this).attr('href');
window.location.hash = target;
$('#testresults').fadeOut('slow', function() {
// complete fadeout, load new content while it's hiding!
$.ajax( {
url : target,
success : function(data) {
$('#testresults').html(data);
$('#testresults').fadeIn();
}
});
});
return false;
})
});
这里如何实现深层链接?
非常感谢, MEM
PS - Darryl E. Clarke 对本次实施的大力支持。 我可以接受那些不好的。
I feel the need to allow the browser back button to work, as well to allow the users to bookmark what they see.
I'm not versatile on Zend routes but, I cannot change that for the moment.
This is the ajax implementation approach that I'm using:
class TestController extends Zend_Controller_Action {
public function init()
{
/* Initialize action controller here */
if ($this->getRequest()->isXMLHttpRequest()) {
$this->_helper->layout()->setLayout('blank');
$logger = $this->getInvokeArg('bootstrap')->getResource('Log');
$logger->debug('AJAX Call');
}
}
public function indexAction()
{
// render the default page
}
public function somethingelseAction()
{
// do something else render something.
}
}
Have my initial view render with a target div, as well as some links... Here's my index.phtml:
<h1>Tests...</h1>
<a class="ajaxloader"
href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'speed'), null, true);?>">Speed</a>
<a class="ajaxloader"
href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'somethingelse'), null, true);?>">Something Else</a>
<div id="testresults">
<h1>Default stuff to show.</h1>
</div>
Some jQuery code to attach to these 'ajaxloader' links and target the results to the 'testresults' div.
$(function() {
$('.ajaxloader').click(function(event) {
var target = $(this).attr('href');
window.location.hash = target;
$('#testresults').fadeOut('slow', function() {
// complete fadeout, load new content while it's hiding!
$.ajax( {
url : target,
success : function(data) {
$('#testresults').html(data);
$('#testresults').fadeIn();
}
});
});
return false;
})
});
How can a deep link be implemented here?
Thanks a lot,
MEM
PS - Good credits for this implementation goes to Darryl E. Clarke.
I can take the bad ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于深层链接:我必须做同样的事情(我们的代码看起来几乎相同!)一段时间后发现 jQuery 地址。不过 IE 有一个问题。其中一项功能导致其停止工作。据我所知,我没有使用该功能,所以我只是让它返回,而不是做它正在做的事情。在当前版本 (1.3.1) 中,它是第 77 行的函数 (_search = function(el))。就像我说的,我只是在函数的顶部放置了一个 return ,现在一切都运行良好。
至于路线...文档应该是您的第一个端口的通话。
我对路由所做的就是在我的引导文件中创建一个 _init 函数并执行类似的操作:
但是将您自己的路由放在一起以满足您的需求(例如您可能不想使用正则表达式路由)。
For the deep linking: I had to do the same thing (our code looks almost identical!) and after a while found jQuery Address. There is a problem with IE, though. One of the functions causes it to stop working. As far as I can tell, I'm not using that feature so I've just got it to return instead of doing whatever it is doing. In the current version (1.3.1) it is the function at line 77 (_search = function(el)). Like I said, I just placed a return at the top of the function and it all works nicely now.
As for routes... The documentation should be your first port of call.
What I do for routes, is in my bootstrap file, create an _init function and do something like:
But put together your own routes to suit your needs (for example you probably don't want to use regex routes).