Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change data-confirm dialog implementation to callback style. #3511

Closed
wants to merge 1 commit into from

Conversation

tanakahisateru
Copy link
Contributor

To create custom dialog for yii.confirm, current design is so inconvenient.

JSe's standard confirm() function can block event handling until user input. But most of custom modal UI (for example using Bootbox.js http://bootboxjs.com/) can't control script processing. I suggest to change handling data-confirm and data-method from return-style to callback-style.

I've succeeded to create my dialog box using Bootstrap's Modal component by my callback version.

<div id="my-confirm" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <p></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary confirm-ok">OK</button>
            </div>
        </div>
    </div>
</div>
var $confirmModal = jQuery("#my-confirm");

$confirmModal.find(".confirm-ok").on("click", function() {
    var action = $confirmModal.data("ok-action");
    if (action !== undefined) {
        $confirmModal.on("hidden.bs.modal", function() {
            $confirmModal.off("hidden.bs.modal", arguments.callee);
            action();
        });
    }
    $confirmModal.modal("hide");
});

yii.confirm = function(message, action) {
    $confirmModal.find(".modal-body p").text(message);
    $confirmModal.data("ok-action", action);
    $confirmModal.modal("show");
};

@tanakahisateru
Copy link
Contributor Author

cool-dialog

<?= Html::a('Do Something', ['my-action', 'id' => $id], [
    'class' => 'btn btn-primary',
    'data' => [
        'confirm' => 'Are you sure you want to do?',
        'method' => 'post',
    ],
]) ?>

@Ragazzo
Copy link
Contributor

Ragazzo commented May 19, 2014

i think this one can be very useful since we have default bootstrap 3 support, also can you include your guide in some of widgets guide?

@johonunu
Copy link
Contributor

Have you tried this:

// ADD BOOTSTRAP DIALOGS TO CONFIRM DIALOGS
yii.allowAction = function ($e) {
  var message = $e.data('confirm');

  if($e.data('confirmed') || message === undefined) return true;

  $.confirm({
      text: message,
      confirm: function(button) {
        $e.data('confirmed','true');
        yii.handleAction($e);
      },
      cancel: function(button) {

      },
      confirmButton: '<span class="glyphicon glyphicon-ok"></span>',
      cancelButton: '<span class="glyphicon glyphicon-remove"></span>',
  });
  return false;
};

@tanakahisateru
Copy link
Contributor Author

I've never thought such. I guessed that handleAction and allowAction are accessed as private. I thought only yii.confirm can be overridden. Calling handleAction in allowAction and ignorering confirm are right way? If so, your way is very cool for me.

I think it's a design issue for users but it also relates to development stability. Though I like clean design passing an action as callback, but in this beta phase of dev, modification may be danger.

@johonunu
Copy link
Contributor

I think it could be done in better way. But this is how I implemented it in my cms, so I wanted to share ;)

@Ragazzo
Copy link
Contributor

Ragazzo commented Jun 1, 2014

any news on this one? Also i would like to see here something with ajax update or ajax submit form, can Yii2 provide it out of the box, so we will not be searching for some good solution?

@tanakahisateru
Copy link
Contributor Author

TODO: checking to use it with Ajax update.

@Ragazzo
Copy link
Contributor

Ragazzo commented Jun 2, 2014

@tanakahisateru could be useful i think . Plus one that used here.

@Ragazzo
Copy link
Contributor

Ragazzo commented Jun 2, 2014

@qiangxue @samdark maybe we should also implement ajax saving for modal too? Something like this one .

@samdark samdark added this to the 2.0 RC milestone Jun 30, 2014
@qiangxue qiangxue closed this in b4c4b40 Sep 7, 2014
@qiangxue
Copy link
Member

qiangxue commented Sep 7, 2014

@tanakahisateru Thanks.
@Ragazzo for ajax form submission, you normally need to add callbacks to handle the ajax return, which is not appropriate to be specified via data attributes.

@Ragazzo
Copy link
Contributor

Ragazzo commented Sep 7, 2014

i was talking only about ajax saving for modals, so they will trigger ajax request and then replace content with the given one and so on . This could be very helpful

cebe added a commit to cebe/yii2 that referenced this pull request Sep 7, 2014
* master: (1112 commits)
  Removed unused "use" statements
  Fixed array syntax
  Removed unused "use" statements
  Перевод runtime-handling-errors.md на русский язык
  Mark tests with problematic DBMS as skipped.
  Fixes yiisoft#3511: Dropped `yii.allowAction()` and modified `yii.confirm()` in `yii.js` to support callbacks
  Update runtime-url-handling.md
  Update runtime-url-handling.md
  Update runtime-url-handling.md
  Update runtime-url-handling.md
  Update runtime-url-handling.md
  Create runtime-url-handling.md
  Fixed test break.
  Fixes yiisoft#3410: yii.activeForm.js now supports adding/removing fields dynamically
  Fixes yiisoft#2914: `ActiveForm::fieldConfig` will be merged recursively with the `$options` parameter in `ActiveForm::field()`
  Fixes yiisoft#4936 [skip ci]
  Reverted/fixed some incorrect changes.
  missing "." sign
  Fixed formatting, initialized options array
  Fixed phpdoc
  ...

Conflicts:
	framework/CHANGELOG.md
	framework/base/Formatter.php
	framework/i18n/Formatter.php
	tests/unit/framework/base/FormatterTest.php
	tests/unit/framework/i18n/FormatterTest.php
@adiramardiani
Copy link

Sorry for my stupid question, I following Yii 2.0: Escape from Default's Yii2 Delete Confirm Box Article and the function is changed now in Dropped yii.allowAction() and modified yii.confirm()

Is there someone here, can explain custom method to override a yii confirm method to integrated with bootbox.js ?

@tanakahisateru
Copy link
Contributor Author

Sorry in only Japanese, I wrote an article how to do it last night.
http://qiita.com/tanakahisateru/items/be28b7bed4566ce8fa99

@adiramardiani
Copy link

thanks @tanakahisateru 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants