I'm trying to make some ajax-functionality in my web application, but I cannot get all puzzle pieces to fit:
我正在尝试在我的Web应用程序中创建一些ajax功能,但我无法获得所有拼图:
I want to add a link that, when clicked upon, will open a new input (text) field that can be filled by the user. In the back-end, I want to do some administration that the link is clicked.
我想添加一个链接,当单击该链接时,将打开一个可由用户填写的新输入(文本)字段。在后端,我想做一些点击链接的管理。
I want to do according to the Zend Framework principles, with using the ajaxLink()
method. Can anyone have an example for me? I've read the official documentation (ZendX_JQuery) but it doesn't fully help me.
我想根据Zend Framework原则,使用ajaxLink()方法。任何人都可以为我做一个例子吗?我已经阅读了官方文档(ZendX_JQuery),但它并没有完全帮助我。
My front-end (view) code looks like this;
我的前端(视图)代码看起来像这样;
<?= $this->ajaxLink("Subscribe", $this->url(array('controller' => 'mycontroller', 'action' => 'action1', 'id' => $event['id'])),
array("beforeSend" => "hide",
"update" => "#pb_" . $event['id'],
'noscript' => false,
'method' => 'POST')); ?>
My back-end code looks like this.
我的后端代码看起来像这样。
public function action1Action()
{
if( !$this->loggedIn || ! $this->athlete) {
$this->_redirect('index');
}
if(! $this->_request->isXmlHttpRequest())
{
//The request was NOT made with JS XmlHttpRequest
die;
}
// Do some administration
// (removed to make this easier in this example)
$pb = new Zend_Form_Element_Text('PB');
$pb->setLabel('PB:')
->addValidator('StringLength', false, array(0,20))
->setRequired(false);
$renderText = $pb->render();
return $renderText;
}
I keep getting errors back that the given method wants to look-up a action1.phtml view script. I'm also not sure if what I try to do with the generation of the form input element works in this way.
我一直收到错误,说明给定的方法想要查找action1.phtml视图脚本。我也不确定我尝试使用表单输入元素的生成是否以这种方式工作。
I found some of the answer in this question, but it's not that elegant (requires an extra parameter in the link and you need another controller) which I don't like.
我在这个问题中找到了一些答案,但它不是那么优雅(在链接中需要一个额外的参数,你需要另一个控制器)我不喜欢。
2 个解决方案
#1
2
You need to turn off the ViewRenderer for this particular action. ZF by default enables an Action Helper called ViewRenderer which assigns a conventionally named view script (in your case, action1.phtml) to a particular action method. Since you're only trying to return a small snippet of text, rather than a full site view, full view rendering isn't necessary. Fortunately, this is easy.
您需要为此特定操作关闭ViewRenderer。默认情况下,ZF启用一个名为ViewRenderer的Action Helper,它将常规命名的视图脚本(在您的情况下为action1.phtml)分配给特定的操作方法。由于您只是尝试返回一小段文本,而不是完整的网站视图,因此不需要全视图渲染。幸运的是,这很容易。
public function action1Action(){
$this->_helper->viewRenderer->setNoRender();
// the rest of your code
}
The full docs are here: http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer
完整的文档在这里:http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer
#2
0
There's nothing to it. All you have to do is point it to where your content is coming from. In your view:
什么都没有。您所要做的就是将其指向您的内容来源。在你看来:
<?= $this->ajaxLink("Example 1","/controller/action1",
array('update' => '#content',
'noscript' => false,
'method' => 'POST')); ?>
In your controller:
在你的控制器中:
echo 'Some Content';
Read this:
http://www.mikaelkael.fr/IMG/pdf/ZendX_Framework_1.7.x_EN.pdf
#1
2
You need to turn off the ViewRenderer for this particular action. ZF by default enables an Action Helper called ViewRenderer which assigns a conventionally named view script (in your case, action1.phtml) to a particular action method. Since you're only trying to return a small snippet of text, rather than a full site view, full view rendering isn't necessary. Fortunately, this is easy.
您需要为此特定操作关闭ViewRenderer。默认情况下,ZF启用一个名为ViewRenderer的Action Helper,它将常规命名的视图脚本(在您的情况下为action1.phtml)分配给特定的操作方法。由于您只是尝试返回一小段文本,而不是完整的网站视图,因此不需要全视图渲染。幸运的是,这很容易。
public function action1Action(){
$this->_helper->viewRenderer->setNoRender();
// the rest of your code
}
The full docs are here: http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer
完整的文档在这里:http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer
#2
0
There's nothing to it. All you have to do is point it to where your content is coming from. In your view:
什么都没有。您所要做的就是将其指向您的内容来源。在你看来:
<?= $this->ajaxLink("Example 1","/controller/action1",
array('update' => '#content',
'noscript' => false,
'method' => 'POST')); ?>
In your controller:
在你的控制器中:
echo 'Some Content';
Read this:
http://www.mikaelkael.fr/IMG/pdf/ZendX_Framework_1.7.x_EN.pdf