Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
fix for preventing accumulation of editable events
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola Simanic committed Jan 29, 2019
1 parent d5e5d0e commit 6f5fe74
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
7 changes: 4 additions & 3 deletions widgets/editable/WhEditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions widgets/editable/assets/bootstrap-editable/js/editable-setup.js
Original file line number Diff line number Diff line change
@@ -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();
}
};

0 comments on commit 6f5fe74

Please sign in to comment.