codedance / jquery.AreYouSure

A light-weight jQuery "dirty forms" Plugin - it monitors html forms and alerts users to unsaved changes if they attempt to close the browser or navigate away from the page. (Are you sure?)
508 stars 145 forks source link

How to invoke AYS when the user clicks on other links on the same application? #78

Open svavantsa opened 9 years ago

svavantsa commented 9 years ago

AYS is getting invoked only when you refresh the page of close the page/tab. But if I am on a data entry form, and I dirtied it, then I click on another link on the same application, AYS is not getting invoked. How can I have that happen?

codedance commented 9 years ago

@svavantsa What browser are you using? The beforeunload event should fire in most browsers any time you exit/leave the page.

One thought, is your page a "single page application" or using push state? If this is the case, then you may need to wire up your own logic to trigger the beforeunload event.

svavantsa commented 9 years ago

Chris,

Thank you for taking the time to look into my question.

I am using all browsers, IE10+, Chrome and FF. And the links I am talking about are those within the same application.

Also, as a separate question, is there a way, I can specify three options/buttons on the AYS dialog? "Save and continue", "Discard and continue", "Go back to the form"?

On Wed, Mar 25, 2015 at 7:02 PM Chris Dance notifications@github.com wrote:

@svavantsa https://github.com/svavantsa What browser are you using? The beforeunload event should fire in most browsers any time you exit/leave the page.

One thought, is your page a "single page application" or using push state? If this is the case, then you may need to wire up your own logic to trigger the beforeunload event.

— Reply to this email directly or view it on GitHub https://github.com/codedance/jquery.AreYouSure/issues/78#issuecomment-86247024 .

codedance commented 9 years ago

OK. So it sounds like you have a single page application? If this is the case you can't reply on the browser to raise the beforeunload event (which performs the check and presents the dialog). Instead you need to work this code yourself, or use a mechanism that may existing in your framework.

e.g.

if ($('#my-form').hasClass('dirty')) {
  // Do something like display message and/or prevent navigation
}

Regarding the question about buttons/messages. The beforeunload event's dialogue design is a browser feature. Unfortunately customization is not possible other than message.

svavantsa commented 9 years ago

Chris, Its fine about the buttons on the dealer.

As for why it's appearing twice, this is a regular ruby on rails app. There are several controller, views etc. It's not a single page app.

Regardless of which browser I use, it appears twice.

On Wed, Mar 25, 2015, 11:22 PM Chris Dance notifications@github.com wrote:

OK. So it sounds like you have a single page application? If this is the case you can't reply on the browser to raise the beforeunload event (which performs the check and presents the dialog). Instead you need to work this code yourself, or use a mechanism that may existing in your framework.

e.g.

if ($('#my-form').hasClass('dirty')) { // Do something like display message or }

Regarding the question about buttons/messages. The beforeunload event's dialogue design is a browser feature. Unfortunately customization is not possible other than message.

— Reply to this email directly or view it on GitHub https://github.com/codedance/jquery.AreYouSure/issues/78#issuecomment-86323106 .

codedance commented 9 years ago

OK. I think I misunderstood the problem. So the issue is that the "are you sure" dialog appears twice? Do you have a public URL I can hit to see the problem? If I can duplicate I may be able to better understand the problem/solution.

svavantsa commented 9 years ago

Yes. That's right. That dialog appears twice. It's still under development so I don't have a public URL yet. Let me send the code to you so you can see it. On Mar 30, 2015 12:58 AM, "Chris Dance" notifications@github.com wrote:

OK. I think I misunderstood the problem. So the issue is that the "are you sure" dialog appears twice? Do you have a public URL I can hit to see the problem? If I can duplicate I may be able to better understand the problem/solution.

— Reply to this email directly or view it on GitHub https://github.com/codedance/jquery.AreYouSure/issues/78#issuecomment-87547245 .

svavantsa commented 9 years ago

$(document).on("page:change", function () { console.log("I am on mainjs.js")

$('form').areYouSure({'message': 'Correct Message!'});
$('.tabs').on('click', function () {
    dirtyCheck()
})
$('.subtabs').on('click', function () {
    dirtyCheck()
})
$('.btn-primary').on('click', function () {
    dirtyCheck()
})

})

function dirtyCheck () { console.log("Inside dirtyCheck function") if ($("#myBody").hasClass('show') || $("#myBody").hasClass('index')) { } else { console.log("You are on a data entry form") if ($('form').hasClass('dirty')){ console.log("You are on a dirty form") var r = confirm("Correct Message 2!") return r } } }

Chris,

See the code above. Basically, the dialog from the first call to AYS runs properly. Just once. But I need to invoke this dirty check when the user clicks on other buttons with specific classes within the application too. Those are the .tabs and the .subtabs classes. Notice how Iam calling the function called dirtyCheck(). See the definition of this function below the call. In this call, the confirm call is the one that runs twice. How can I avoid this?

Thank You,

Surya Avantsa

http://vame.me/suryaavantsa

On Mon, Mar 30, 2015 at 6:41 AM, Surya Avantsa svavantsa@gmail.com wrote:

Yes. That's right. That dialog appears twice. It's still under development so I don't have a public URL yet. Let me send the code to you so you can see it. On Mar 30, 2015 12:58 AM, "Chris Dance" notifications@github.com wrote:

OK. I think I misunderstood the problem. So the issue is that the "are you sure" dialog appears twice? Do you have a public URL I can hit to see the problem? If I can duplicate I may be able to better understand the problem/solution.

— Reply to this email directly or view it on GitHub https://github.com/codedance/jquery.AreYouSure/issues/78#issuecomment-87547245 .

svavantsa commented 9 years ago

Instead of using the windows confirm call, I tried to trigger the event using your idea.

$('form').trigger('checkform.areYouSure');

But that never gets triggered.

Thank You,

Surya Avantsa

http://vame.me/suryaavantsa

On Mon, Mar 30, 2015 at 2:18 PM, Surya Avantsa svavantsa@gmail.com wrote:

$(document).on("page:change", function () { console.log("I am on mainjs.js")

$('form').areYouSure({'message': 'Correct Message!'});
$('.tabs').on('click', function () {
    dirtyCheck()
})
$('.subtabs').on('click', function () {
    dirtyCheck()
})
$('.btn-primary').on('click', function () {
    dirtyCheck()
})

})

function dirtyCheck () { console.log("Inside dirtyCheck function") if ($("#myBody").hasClass('show') || $("#myBody").hasClass('index')) { } else { console.log("You are on a data entry form") if ($('form').hasClass('dirty')){ console.log("You are on a dirty form") var r = confirm("Correct Message 2!") return r } } }

Chris,

See the code above. Basically, the dialog from the first call to AYS runs properly. Just once. But I need to invoke this dirty check when the user clicks on other buttons with specific classes within the application too. Those are the .tabs and the .subtabs classes. Notice how Iam calling the function called dirtyCheck(). See the definition of this function below the call. In this call, the confirm call is the one that runs twice. How can I avoid this?

Thank You,

Surya Avantsa

http://vame.me/suryaavantsa

On Mon, Mar 30, 2015 at 6:41 AM, Surya Avantsa svavantsa@gmail.com wrote:

Yes. That's right. That dialog appears twice. It's still under development so I don't have a public URL yet. Let me send the code to you so you can see it. On Mar 30, 2015 12:58 AM, "Chris Dance" notifications@github.com wrote:

OK. I think I misunderstood the problem. So the issue is that the "are you sure" dialog appears twice? Do you have a public URL I can hit to see the problem? If I can duplicate I may be able to better understand the problem/solution.

— Reply to this email directly or view it on GitHub https://github.com/codedance/jquery.AreYouSure/issues/78#issuecomment-87547245 .

svavantsa commented 9 years ago

Chris,

I resolved this issue. Basically the script was getting included twice in the rails application, once by default and another time when I explicitly included it in other parts, inadvertently. When I noticed that and removed the duplicate references, the issue automatically went away. ᐧ

Thank You,

Surya Avantsa

http://vame.me/suryaavantsa

On Mon, Mar 30, 2015 at 3:05 PM, Surya Avantsa svavantsa@gmail.com wrote:

Instead of using the windows confirm call, I tried to trigger the event using your idea.

$('form').trigger('checkform.areYouSure');

But that never gets triggered.

Thank You,

Surya Avantsa

http://vame.me/suryaavantsa

On Mon, Mar 30, 2015 at 2:18 PM, Surya Avantsa svavantsa@gmail.com wrote:

$(document).on("page:change", function () { console.log("I am on mainjs.js")

$('form').areYouSure({'message': 'Correct Message!'});
$('.tabs').on('click', function () {
    dirtyCheck()
})
$('.subtabs').on('click', function () {
    dirtyCheck()
})
$('.btn-primary').on('click', function () {
    dirtyCheck()
})

})

function dirtyCheck () { console.log("Inside dirtyCheck function") if ($("#myBody").hasClass('show') || $("#myBody").hasClass('index')) { } else { console.log("You are on a data entry form") if ($('form').hasClass('dirty')){ console.log("You are on a dirty form") var r = confirm("Correct Message 2!") return r } } }

Chris,

See the code above. Basically, the dialog from the first call to AYS runs properly. Just once. But I need to invoke this dirty check when the user clicks on other buttons with specific classes within the application too. Those are the .tabs and the .subtabs classes. Notice how Iam calling the function called dirtyCheck(). See the definition of this function below the call. In this call, the confirm call is the one that runs twice. How can I avoid this?

Thank You,

Surya Avantsa

http://vame.me/suryaavantsa

On Mon, Mar 30, 2015 at 6:41 AM, Surya Avantsa svavantsa@gmail.com wrote:

Yes. That's right. That dialog appears twice. It's still under development so I don't have a public URL yet. Let me send the code to you so you can see it. On Mar 30, 2015 12:58 AM, "Chris Dance" notifications@github.com wrote:

OK. I think I misunderstood the problem. So the issue is that the "are you sure" dialog appears twice? Do you have a public URL I can hit to see the problem? If I can duplicate I may be able to better understand the problem/solution.

— Reply to this email directly or view it on GitHub https://github.com/codedance/jquery.AreYouSure/issues/78#issuecomment-87547245 .