Zend Form Element with Javascript - 装饰器、视图助手或视图脚本? [英] Zend Form Element with Javascript - Decorator, View Helper or View Script?
问题描述
我想向 Zend_Form_Element_Text 添加一些 javacsript.
I want to add some javacsript to a Zend_Form_Element_Text .
起初我认为装饰器是最好的方法,但由于它只是一个脚本(标记不会改变)那么也许视图助手更好?还是视图脚本?
At first I thought a decorator would be the best way to do it, but since it is just a script (the markup doesn't change) then maybe a view helper is better? or a view script?
似乎它们都是出于相同的目的(关于表单元素).
It seems like they are all for the same purpose (regarding a form element).
我要添加的 javascript 不是事件(例如更改、点击等).我可以使用 headScript() 轻松添加它,但我想让它重用,这就是我想到装饰器/视图助手的原因.我只是不清楚它们之间的区别.
The javascript I want to add is not an event (e.g. change, click, etc.). I can add it easily with headScript() but I want to make it re-usable , that's why I thought about a decorator/view helper. I'm just not clear about the difference between them.
这种情况下的最佳做法是什么?优势?
What is the best practice in this case? advantages?
更新:似乎最佳实践是使用视图脚本中的视图助手,所以装饰器会更合适?
UPDATE: Seems like the best practice is to use view helpers from view scripts , so decorators would be a better fit?
谢谢.
推荐答案
你可以通过扩展 Zend_From_Decorator_Abstract
来创建你自己的装饰器,并在它的 render()
方法中生成你的代码片段:
You could create your own decorator by extending Zend_From_Decorator_Abstract
and generate your snippet in it's render()
method :
class My_Decorator_FieldInitializer extends Zend_Form_Decorator_Abstract {
public function render($content){
$separator = $this->getSeparator();
$element = $this->getElement();
$output = '<script>'.
//you write your js snippet here, using
//the data you have in $element if you need
.'</script>';
return $content . $separator . $output;
}
}
如果您需要更多详细信息,请在评论中询问,我会编辑此答案.我没有测试这段代码.
If you need more details, ask for it in a comment, i'll edit this answer. And I didn't test this code.
这篇关于Zend Form Element with Javascript - 装饰器、视图助手或视图脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!