diff --git a/widgets/editable/WhEditable.php b/widgets/editable/WhEditable.php index 42b6fb9..8567703 100644 --- a/widgets/editable/WhEditable.php +++ b/widgets/editable/WhEditable.php @@ -502,8 +502,8 @@ public function registerClientScript() //wrap in anonymous function for live update if ($this->liveTarget) { - $script .= "\n $('body').on('ajaxUpdate.editable', function(e){ if(e.target.id == '" . $this->liveTarget . "'){yiiEditable();}});"; - $script = "(function yiiEditable() {\n " . $script . "\n}());"; + $id = $this->liveTarget . $this->getId(); + $script .= "window.WhEditableSetup.set('{$id}', '{$this->liveTarget}', function() {\n {$script}\n});"; } Yii::app()->getClientScript()->registerScript(__CLASS__ . '-' . $selector, $script); @@ -519,10 +519,11 @@ public function registerAssets() { $cs = Yii::app()->getClientScript(); - $path = __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'bootstrap-editable'; $assetsUrl = $this->getAssetsUrl($path); + $cs->registerScriptFile($assetsUrl . '/js/editable-setup.js', CClientScript::POS_BEGIN); + //register assets $cs->registerCssFile($assetsUrl . '/css/bootstrap-editable.css'); $cs->registerScriptFile($assetsUrl . '/js/bootstrap-editable.js', CClientScript::POS_END); diff --git a/widgets/editable/assets/bootstrap-editable/js/editable-setup.js b/widgets/editable/assets/bootstrap-editable/js/editable-setup.js new file mode 100644 index 0000000..0a85317 --- /dev/null +++ b/widgets/editable/assets/bootstrap-editable/js/editable-setup.js @@ -0,0 +1,36 @@ +/** + * Script which should prevent accumulation of same events. + */ + +window.WhEditableSetup = window.WhEditableSetup || { + _targets: {}, + _initialized: false, + _initialize: function() { + if (this._initialized) { + return; + } + + var targets = this._targets; + + $('body').off('ajaxUpdate.editable').on('ajaxUpdate.editable', function(e) { + for(var target in targets) { + if (!targets.hasOwnProperty(target)) { + continue; + } + + var targetInfo = targets[target]; + + if (e.target.id == targetInfo.target) { + targetInfo.runner(); + } + } + }); + + this._initialized = true; + }, + set: function(widgetId, target, runner) { + this._initialize(); + this._targets[widgetId] = {target: target, runner: runner}; + runner(); + } +}; \ No newline at end of file