Skip to content

Instantly share code, notes, and snippets.

@deanapeterson
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save deanapeterson/5b28204e9aadb225015a to your computer and use it in GitHub Desktop.

Select an option

Save deanapeterson/5b28204e9aadb225015a to your computer and use it in GitHub Desktop.

Revisions

  1. deanapeterson revised this gist Jan 14, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions alternate-ui-router-event-binding.js
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,10 @@ angular.module("app", ["ui.router"])

    },
    //fired only when state is the "from" state
    onFromStateChangeStart : function(event){
    event.preventDefault();
    onFromStateChangeStart : function(event, $state, toState){
    if($state.includes(toState) === false){
    event.preventDefault();
    }
    }
    }
    });
  2. deanapeterson revised this gist Jan 14, 2015. 1 changed file with 20 additions and 18 deletions.
    38 changes: 20 additions & 18 deletions alternate-ui-router-event-binding.js
    Original file line number Diff line number Diff line change
    @@ -28,23 +28,25 @@ angular.module("app", ["ui.router"])
    })
    .run(function($rootScope, $injector){

    $rootScope.$on("$stateChangeStart", function(event, to, toParams, from, fromParams){
    var stateChangeArgs = {
    event : event,
    to : to,
    toParams : toParams,
    from : from,
    fromParams : fromParams
    };

    if(to.data.onToStateChangeStart){
    $injector.invoke(to.data.onToStateChangeStart, {}, stateChangeArgs);
    }

    if(from.data.onFromStateChangeStart){
    $injector.invoke(from.data.onFromStateChangeStart, {}, stateChangeArgs);
    }

    });
    $rootScope.$on("$stateChangeSuccess", function(event, toState, toParams, fromState, fromParams){
    var stateChangeArgs = {
    event : event,
    toState : toState,
    toParams : toParams,
    fromState : fromState,
    fromParams : fromParams
    };
    var toStateData = toState.data || {};
    var fromStateData = fromState.data || {};

    if(toStateData.onToStateChangeStart){
    $injector.invoke(toStateData.onToStateChangeStart, {}, stateChangeArgs);
    }

    if(fromStateData.onFromStateChangeStart){
    $injector.invoke(fromStateData.onFromStateChangeStart, {}, stateChangeArgs);
    }

    });

    });
  3. deanapeterson revised this gist Jan 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion alternate-ui-router-event-binding.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    *
    * I like this because the we can run all handlers through one event binding
    * and make the state definition object for self-contained.
    * /
    **/

    angular.module("app", ["ui.router"])
    .config(function(){
  4. deanapeterson renamed this gist Jan 12, 2015. 1 changed file with 0 additions and 0 deletions.
  5. deanapeterson revised this gist Jan 12, 2015. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions ui-router-events.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,14 @@
    /**
    * Similar to the idea of adding 'rules' to a states data object, I add injectible
    * handlers keyed to the specific stateChange relationship:
    *
    * 'onToStateChangeStart/Success' - fired when the state is the destination
    * 'onFromStateChangeStart/Success' - fired when the state is the previous
    *
    * I like this because the we can run all handlers through one event binding
    * and make the state definition object for self-contained.
    * /

    angular.module("app", ["ui.router"])
    .config(function(){
    $stateProvider
  6. deanapeterson revised this gist Jan 12, 2015. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions ui-router-events.js
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,11 @@ angular.module("app", ["ui.router"])
    .state("root", {
    template : "<div ui-view></div>",
    data : {
    onStateChangeStart : function(toParams, fromParams){

    },
    //fired only when state is the "to" state
    onToStateChangeStart : function(to, from){

    },
    //fired only when state is the "from" state
    onFromStateChangeStart : function(event){
    event.preventDefault();
    }
    @@ -27,10 +26,6 @@ angular.module("app", ["ui.router"])
    fromParams : fromParams
    };

    if(to.data.onStateChangeStart){
    $injector.invoke(to.data.onStateChangeStart, {}, stateChangeArgs);
    }

    if(to.data.onToStateChangeStart){
    $injector.invoke(to.data.onToStateChangeStart, {}, stateChangeArgs);
    }
  7. deanapeterson revised this gist Jan 12, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ui-router-events.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,8 @@ angular.module("app", ["ui.router"])
    event.preventDefault();
    }
    }
    })
    });
    })
    .run(function($rootScope, $injector){

    $rootScope.$on("$stateChangeStart", function(event, to, toParams, from, fromParams){
  8. deanapeterson renamed this gist Jan 12, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. deanapeterson created this gist Jan 12, 2015.
    43 changes: 43 additions & 0 deletions new_gist_file.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    angular.module("app", ["ui.router"])
    .config(function(){
    $stateProvider
    .state("root", {
    template : "<div ui-view></div>",
    data : {
    onStateChangeStart : function(toParams, fromParams){

    },
    onToStateChangeStart : function(to, from){

    },
    onFromStateChangeStart : function(event){
    event.preventDefault();
    }
    }
    })
    .run(function($rootScope, $injector){

    $rootScope.$on("$stateChangeStart", function(event, to, toParams, from, fromParams){
    var stateChangeArgs = {
    event : event,
    to : to,
    toParams : toParams,
    from : from,
    fromParams : fromParams
    };

    if(to.data.onStateChangeStart){
    $injector.invoke(to.data.onStateChangeStart, {}, stateChangeArgs);
    }

    if(to.data.onToStateChangeStart){
    $injector.invoke(to.data.onToStateChangeStart, {}, stateChangeArgs);
    }

    if(from.data.onFromStateChangeStart){
    $injector.invoke(from.data.onFromStateChangeStart, {}, stateChangeArgs);
    }

    });

    });