Zend Form Element with Javascript - 装饰器、视图助手还是视图脚本?

发布于 2024-12-05 01:57:43 字数 330 浏览 0 评论 0原文

我想向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹏半生如梦愿梦如真 2024-12-12 01:57:43

您可以通过扩展 Zend_From_Decorator_Abstract 来创建您自己的装饰器,并在其 render() 方法中生成代码片段:

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;
    }
}

如果您需要更多详细信息,请在评论中询问,我会编辑这个答案。而且我没有测试这段代码。

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.

夏至、离别 2024-12-12 01:57:43

使用 setAttrib 函数。

例如:-

$element = new Zend_Form_Element_Text('test');
$element->setAttrib('onclick', 'alert("Test")');

Use setAttrib function.

eg:-

$element = new Zend_Form_Element_Text('test');
$element->setAttrib('onclick', 'alert("Test")');
静若繁花 2024-12-12 01:57:43

我实际上并没有看到哪里需要装饰器、视图帮助器或视图脚本。

如果我想将一些客户端行为附加到表单元素,我可能会使用 $elt->setAttrib('class', 'someClass')$ 设置属性elt->setAttrib('id', 'someId'),我的脚本可以附加的一些钩子。然后我将侦听器/处理程序添加到这些目标元素。

例如,对于使用 jQuery 的点击处理程序,它会类似于:

(function($){
    $(document).ready(function(){
        $('.someClass').click(function(e){
            // handle the event here
        });
    });
})(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:

(function($){
    $(document).ready(function(){
        $('.someClass').click(function(e){
            // handle the event here
        });
    });
})(jQuery);

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.

呆橘 2024-12-12 01:57:43

“标记不会改变”,Yap,

但我喜欢添加一些 javascript 函数,抛出 ZendForm Element:

$text_f = new Zend_Form_Element_Text("text_id");
$text_f->setAttrib('OnChange', 'someFunction($(this));');

最好的方法是,如果您正在与一个团队合作,那么所有人都应该使用相同的代码标准。对于我和我的团队来说,这就是上面的代码。

"the markup doesn't change", Yap,

but I like to add some javascript function throw ZendForm Element:

$text_f = new Zend_Form_Element_Text("text_id");
$text_f->setAttrib('OnChange', 'someFunction($(this));');

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文