Zend Form Element with Javascript - 装饰器、视图助手还是视图脚本?
我想向 Zend_Form_Element_Text 添加一些 javacsript 。
起初我认为装饰器是最好的方法,但由于它只是一个脚本(标记不会改变),那么也许视图助手更好?或者视图脚本?
看起来它们都是为了相同的目的(关于表单元素)。
我要添加的javascript不是事件(例如更改、单击等)。我可以使用 headScript() 轻松添加它,但我想让它可重用,这就是我考虑装饰器/视图助手的原因。我只是不清楚它们之间的区别。
在这种情况下,最佳实践是什么?优点?
更新:似乎最佳实践是使用视图脚本中的视图助手,所以装饰器会更合适?
谢谢。
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).
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过扩展
Zend_From_Decorator_Abstract
来创建您自己的装饰器,并在其render()
方法中生成代码片段:如果您需要更多详细信息,请在评论中询问,我会编辑这个答案。而且我没有测试这段代码。
You could create your own decorator by extending
Zend_From_Decorator_Abstract
and generate your snippet in it'srender()
method :If you need more details, ask for it in a comment, i'll edit this answer. And I didn't test this code.
使用 setAttrib 函数。
例如:-
Use setAttrib function.
eg:-
我实际上并没有看到哪里需要装饰器、视图帮助器或视图脚本。
如果我想将一些客户端行为附加到表单元素,我可能会使用
$elt->setAttrib('class', 'someClass')
或$ 设置属性elt->setAttrib('id', 'someId')
,我的脚本可以附加的一些钩子。然后我将侦听器/处理程序添加到这些目标元素。例如,对于使用 jQuery 的点击处理程序,它会类似于:
好处是它不引人注目,因此标记保持干净。希望 javascript 是一个增强功能——而不是功能的关键部分——所以它会优雅地降级。
也许您的意思是这个 javascript 段本身需要可以在不同的元素标识符之间重用 - 在本例中为
someClass
。在这种情况下,您可以简单地编写一个接受 CSS 类名作为参数的视图助手。I'm not actually seeing where this needs to be a decorator or a view-helper or a view-script.
If I wanted to attach some client-side behavior to a form element, I'd probably set an attribute with
$elt->setAttrib('class', 'someClass')
or$elt->setAttrib('id', 'someId')
, some hook onto which my script can attach. Then I'd add listeners/handlers to those targeted elements.For example, for a click handler using jQuery , it would be something like:
The benefit is that it is unobtrusive, so the markup remains clean. Hopefully, the javascript is an enhancement- not a critical part of the functionality - so it degrades gracefully.
Perhaps you mean that this javascript segment itself needs to be reusable across different element identifiers -
someClass
, in this example. In this case, you could simply write a view-helper that accepts the CSS class name as the parameter.“标记不会改变”,Yap,
但我喜欢添加一些 javascript 函数,抛出 ZendForm Element:
最好的方法是,如果您正在与一个团队合作,那么所有人都应该使用相同的代码标准。对于我和我的团队来说,这就是上面的代码。
"the markup doesn't change", Yap,
but I like to add some javascript function throw ZendForm Element:
The best way is if you are working with a team, where all of you should use same code standard. For me and my team this is the code above.