Skip to content

Commit

Permalink
Fixed error for the use of multiple nested widgets with zero initial …
Browse files Browse the repository at this point in the history
…elements
  • Loading branch information
wbraganca committed Feb 23, 2015
1 parent 2a6c420 commit 97eab95
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 124 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ dev-master
----------


version 2.0.1
-------------
**Date:** 23-Fev-2015
- bug: Fixed error for the use of multiple nested widgets with zero initial elements


version 2.0.0
-------------
**Date:** 22-Fev-2015
Expand Down
34 changes: 32 additions & 2 deletions DynamicFormWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
class DynamicFormWidget extends \yii\base\Widget
{
const HASH_VAR_BASE_NAME = 'dynamicform_';
/**
* @var string
*/
Expand Down Expand Up @@ -72,6 +73,10 @@ class DynamicFormWidget extends \yii\base\Widget
* @var string
*/
private $_insertPositions = ['bottom', 'top'];
/**
* @var string the hashed global variable name storing the pluginOptions
*/
private $_hashVar;

/**
* Initializes the widget
Expand Down Expand Up @@ -133,6 +138,13 @@ protected function initOptions()
ob_implicit_flush(false);
}

protected function registerOptions($view)
{
$encOptions = Json::encode($this->_options);
$this->_hashVar = DynamicFormWidget::HASH_VAR_BASE_NAME . hash('crc32', $encOptions);
$view->registerJs("var {$this->_hashVar} = {$encOptions};\n", $view::POS_HEAD);
}

/**
* Registers the needed assets
*/
Expand All @@ -141,7 +153,25 @@ public function registerAssets()
$view = $this->getView();
DynamicFormAsset::register($view);
$options = Json::encode($this->_options);
$view->registerJs('$(".' . $this->widgetContainer . '").yiiDynamicForm(' .$options .');', $view::POS_LOAD);
$this->registerOptions($view);

$js = 'jQuery("#' . $this->formId . '").yiiDynamicForm(' . $this->_hashVar .');' . "\n";
$view->registerJs($js, $view::POS_READY);

// add a click handler for the clone button
$js = 'jQuery("#' . $this->formId . '").on("click", "' . $this->insertButton . '", function(e) {'. "\n";
$js .= " e.preventDefault();\n";
$js .= ' jQuery(".' . $this->widgetContainer . '").triggerHandler("beforeInsert", [jQuery(this)]);' . "\n";
$js .= ' jQuery(".' . $this->widgetContainer . '").yiiDynamicForm("addItem", '. $this->_hashVar . ", e, jQuery(this));\n";
$js .= "});\n";
$view->registerJs($js, $view::POS_READY);

// add a click handler for the remove button
$js = 'jQuery("#' . $this->formId . '").on("click", "' . $this->deleteButton . '", function(e) {'. "\n";
$js .= " e.preventDefault();\n";
$js .= ' jQuery(".' . $this->widgetContainer . '").yiiDynamicForm("deleteItem", '. $this->_hashVar . ", e, jQuery(this));\n";
$js .= "});\n";
$view->registerJs($js, $view::POS_READY);
}

public function run()
Expand All @@ -159,7 +189,7 @@ public function run()
}

$this->registerAssets();
echo Html::tag('div', $content, ['class' => $this->widgetContainer, 'data-dynamicform' => $this->widgetContainer]);
echo Html::tag('div', $content, ['class' => $this->widgetContainer, 'data-dynamicform' => $this->_hashVar]);
}

private function removeItems($content)
Expand Down
Loading

0 comments on commit 97eab95

Please sign in to comment.