Giter VIP home page Giter VIP logo

meteor-before-leave-previous's Introduction

Iron Router beforeLeavePrevious

IronRouter provides us with plenty of options to hook into the loading of a new template once the old one has been unloaded, but nothing to allow us to hook into (and potentially block) navigation before unloading the old template.

This beforeLeavePrevious package allows you to stop navigation to a particular page (or any page) until all the hooks have been satisfied (return true). The package's original use case was to display a bootstrap.js modal that prompts for payment before navigating to a restricted area of the app.

The package can also be useful for other asynchronous confirm / subscribe dialogue boxes and animation hooks (i.e. the animations must complete and return ready before navigation begins). It is designed for use with reactive functions that conditionally return true. If you just want to run some code before changing routes you should use one of the inbuilt unload hooks or onBeforeAction etc.

Important beforeLeavePrevious hooks have no effect on a route that is loaded by directly inputting its URL / arriving via an external link. It only takes effect when navigating via Router.go() โ€“ including all a[href] links โ€“ within your app. If you want to perform the same checks upon loading the new route, reference your beforeLeavePrevious function in your route's onBeforeAction hook.

var myBeforeLeavePrevious = function() {
	return Session.equals("dontNavigateUntilIReturn", true);
}

Router.map(function(){
	// Set up a hook for a particular route
	this.route("route1", {
		template: "template",
		beforeLeavePrevious: myBeforeLeavePrevious
	}
})

MyRouteController = RouteController.extend({
	// Affects all routes attached to this route controller
	beforeLeavePrevious: function () {
		return Meteor.user().subscription.payment_status === "ok";
	}
});

Router.beforeLeavePrevious = function () {
	// The router will do this before navigating to ANY internal route

	// Do some animation, tracking or cleanup code here

	return true; // You must always return a value
};

Usage

Add a hook to a route, controller, or globally, as in the examples above. A hook should either create a reactive context, e.g. include Session.get() or Cursor.find() etc., a future (as yet untested), or just literally return true. Once all hooks are satisfied the Router will automatically navigate to the requested page.

meteor-before-leave-previous's People

Contributors

ephemer avatar nerdmed avatar smowden avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

smowden

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.