Giter Club home page Giter Club logo

ember-onbeforeunload's Introduction

ember-onbeforeunload

npm Version Build Status Ember Observer Score

An add-on to conditionally prompt the user when transitioning between routes or closing the browser.

Installation

This library is tested against Ember 1.13.x and Ember 2.x.

ember install ember-onbeforeunload

Usage

To get started, mix the ConfirmationMixin into your Ember.Route. By default, the user will be prompted beforeunload any time the model for the route hasDirtyAttributes (docs).

// app/routes/foo.js
import Ember from 'ember';
import ConfirmationMixin from 'ember-onbeforeunload/mixins/confirmation';

export default Ember.Route.extend(ConfirmationMixin, { });

Customization

This addon tries to provide sane defaults, but it also exposes all of the internals for customization.

Confirmation Message

You can customize the message displayed in the confirmation dialog by overriding the confirmationMessage property. You can either pass a hard-coded string, or use a function.

export default Ember.Route.extend(ConfirmationMixin, {
  confirmationMessage: 'Are you sure?',
});
export default Ember.Route.extend(ConfirmationMixin, {
  confirmationMessage(model) {
    return `Are you sure you want to unload ${model.name}?`;
  }
});
export default Ember.Route.extend(ConfirmationMixin, {
  i18n: service(), // see ember-i18n
  confirmationMessage() {
    return this.get('i18n').t('myTranslation');
  }
});

isPageDirty logic

If you do not sure Ember Data, or you have other logic to determine whether or not the page is dirty, you can override the isPageDirty method.

export default Ember.Route.extend(ConfirmationMixin, {
  isPageDirty(/* model */) {
    const isDirty = true; // your logic here
    return isDirty;
  }
});

Allow Dirty Transitions

By default, we allow navigating within the hierarchy of route you mix the ConfirmationMixin into. For example, navigating from myroute.index to myroute.index.subroute would not check isPageDirty. If you have other logic that determines when a dirty transition should be allowed, you can override shouldCheckIsPageDirty.

export default Ember.Route.extend(ConfirmationMixin, {
  shouldCheckIsPageDirty(transition) {
    const isChildRouteTransition = this._super(...arguments);

    if (transition.targetName === 'some-exempt-route') {
      return true;
    } else {
      return isChildRouteTransition;
    }
  }
});

onUnload logic

If you have some custom logic you'd like to execute when your route is unloaded, you can tie into the onUnload function. By default, this function is a no-op.

export default Ember.Route.extend(ConfirmationMixin, {
  onUnload() {
    // my custom unload logic
  }
});

Upgrading

This library underwent major API changes with version 1.0.0. For information on how to upgrade, please check out UPGRADING.md.

Issues

Found a bug? Please report it!

Development Instructions

Installing

  • git clone this repository
  • npm install
  • bower install

Linting

  • npm run lint:js
  • npm run lint:js -- --fix

Running tests

  • ember test – Runs the test suite on the current Ember version
  • ember test --server – Runs the test suite in "watch mode"
  • npm test – Runs ember try:each to test your addon against multiple Ember versions

Running the dummy application

For more information on using ember-cli, visit http://www.ember-cli.com/.

ember-onbeforeunload's People

Contributors

jasonmit avatar blimmer avatar dependabot[bot] avatar tylerturdenpants avatar ronco avatar krasnoukhov avatar

Stargazers

Nguyen Viet Manh avatar Roman avatar Alex Strizhak avatar Yusuf SAĞDIÇ avatar Marc Lundgren avatar Eduardo Weiland avatar Maros Hluska avatar Chris Devor avatar Ralf Crisostomo avatar Jacek Bandura avatar Matt Gardner avatar Christian Spanring avatar Daniel Jacob Archer avatar johannes avatar Dean M avatar Evan Sims avatar Jan Buschtöns avatar Jordan Nemrow avatar Pete Watters avatar Martin Malinda avatar Michał Olejniczak avatar Tom Wayson avatar Maarten Parmentier avatar  avatar Sean Goresht avatar Jeroen Overschie avatar James Dixon avatar  avatar James Fuller avatar Dave Lowensohn avatar

Watchers

James Cloos avatar  avatar  avatar

ember-onbeforeunload's Issues

Missing license

I wanted to use your code - it works well, but there's no LICENSE file or mention in README. Could you add a license please?

canUnload and allowUnload is confusing

These two terms are so close and it's a bit confusing as to what they do. This is how it works currently:

allowUnload

The return of this function says whether or not we should call canUnload(). Its default behavior is to not check the result of the canUnload method if transitioning to a child route of the route to which the ConfirmationMixin is applied.

canUnload

The return of this function says whether or not the page has dirty attributes that we should warn the user about via the onbeforeunload.

Proposal

As part of a breaking release, we change allowUnload to shouldCheckPageIsDirty and canUnload to pageIsDirty. IMO this is much clearer as to what each of the functions is doing.

Perhaps as part of this release we could also consider #10 since we'll already be breaking stuff.

@jasonmit thoughts?

Turn on TravisCI

We currently have two tests (yay!), but Travis isn't automatically running.

Transition does not call onUnload

@jasonmit first thank you for this trivial looking yet complicated support for detecting when transitioning away from current view. On surface it looks easy but due to browser differences it is a bit pain.

When I navigate to a completely different route (not sub-route) and answer "yes" to the dialog onUnload is not called. I believe this is because to the browser it's not unloading anything. I was able to fix this by adding this.onUnload() after

. So there I added else { this.onUnload(); }.

checks for dirty page when shouldCheckIsPageDirty returns false

The ember-onbeforeunload mixin will ONLY check for the dirty page attribute via isPageDirty when shouldCheckIsPageDirty returns false. This seems completely counter-intuitive. Why would we not check for a dirty page when shouldCheckIsPageDirty returns true?

The logic that does this is here. This constantly confuses me and my team because it seems completely backwards.

I am happy to open a PR fixing this @blimmer, would that be okay? Though, this could break any applications currently using this addon if their addon manager specification is general enough to catch the version.

Removing controller.isDirty default logic

To be good Ember community members, we should discourage the use of Ember.Controller.

It would be a breaking change, but perhaps we should guide users to provide their own canUnload or have logic that utilizes this.modelFor(this.routeName) instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.