1
我正在與Zend_dojo_form戰鬥。如何在zend dojo窗體中添加javascript函數?
下面是代碼(部分):
class Application_Form_RegistrationForm extends Zend_Dojo_Form {
private $_user;
private $_admin;
private $_teamadmin;
private $_newuser;
private $_redirect;
public function __construct($admin = false, $user = null, $redirect = '') {
//blablabla
parent::__construct();
}
public function init() {
Zend_Dojo::enableForm($this);
$this->setMethod('post');
$this->setAttribs(array(
'id' => 'formRegistration',
'name' => 'formRegistration',
));
//some decorators
if ($this->_admin) {
$this->addElement(
//blabla + inline javascript
'onChange' => "if(this == ".E_UserRole::OPERATOR.") dojo.query(\".perms\").style({ display:\"block\" }); else dojo.query(\".perms\").style({ display:\"none\" }); "
)
);
}
if (Application_Manager_Login::hasRole(E_UserRole::ADMIN)) {
//add some display:none elements
$permission_decorators = array(
"DijitElement",
"Errors",
array(array("data" => "HtmlTag"), array("tag" => "td", "class" => "perms", "style"=> "display:none",)),
array("Label", array("tag" => "td", "class" => "perms", "style"=> "display:none", 'escape' => false, 'requiredSuffix' => ' <span class="red">*</span>')),
array(array("row" => "HtmlTag"), array("tag" => "tr"))
);
//hidden element
$this->addElement(
'CheckBox',
'permission_content',
array(
'decorators' => $permission_decorators,
'label' => 'Gestione contenuti',
'checkedValue' => true,
'uncheckedValue' => false,
'checked' => $this->_user->permission_content,
)
);
}
//submit button and other stuff
}
}
正如你可以看到我已經把一些內嵌的JavaScript來顯示/隱藏某些選項時USER_ROLE變化。
現在情況有點複雜。我可以繼續寫內聯的JavaScript,但我想在開頭聲明一個js函數,並從onchange事件中調用它。
由於這種形式是由多個控制器稱爲我不會手動每一個
new RegistrationForm();
被調用時添加該功能。
你不能在外部JS文件中不顯眼嗎? –
@TimFountain我找到了解決方案。外化js不是我想要的,因爲我不在視圖中。我要寫解決方案。 –