Ember.js Router: How to animate state transitions

后端 未结 6 647
情歌与酒
情歌与酒 2020-12-29 22:22

Has somebody found a good way to animate state transitions?

The router immediately removes the view from the DOM. The problem with that is that I can\'t defer that u

相关标签:
6条回答
  • 2020-12-29 22:47

    Ran into this same requirement on my app. Tried Ember Animated Outlet, but didn't give the granularity I needed (element specific animations).

    The solution that worked for me was as follows --

    Change linkTo to be an action

    {{#linkTo "todos"}}<button>Todos</button>{{/linkTo}}
    

    Becomes...

    <a href="#/todos" {{action "goToTodos"}}><button>Todos</button></a>
    

    Create Method for goToTodos in current controller

    App.IndexController = Ember.Controller.extend({
        goToTodos: function(){
    
            // Get Current 'this' (for lack of a better solution, as it's late)
                var holdThis = this;
    
            // Do Element Specific Animation Here
                $('#something').hide(500, function(){
    
                    // Transition to New Template
                        holdThis.transitionToRoute('todos');
    
                });
    
        }
    });
    

    Finally -- To animate in elements on the Todos Template, use didInsertElement on the view

    App.TodosView = Ember.View.extend({
        didInsertElement: function(){
    
            // Hide Everything
                this.$().hide();
    
            // Do Element Specific Animations Here
                $('#something_else').fadeIn(500);
    
        }
    });
    

    So far, this is the most elegant solution I've found for element specific animations on transition. If there is anything better, would love to hear!

    0 讨论(0)
  • 2020-12-29 22:51
    App.SomeView = Ember.View.extend({
      didInsertElement: function(){
       //called on creation
       this.$().hide().fadeIn(400);
      },
      willDestroyElement: function(){
       //called on destruction
       this.$().slideDown(250)
      }
    });
    
    0 讨论(0)
  • 2020-12-29 22:57

    I'll expand on Lesyk's answer. If you need to apply it to multiple views in a DRY way, you can create a customization class like this:

    App.CrossfadeView = {
      didInsertElement: function(){
        //called on creation
        this.$().hide().fadeIn(400);
      },
      willDestroyElement: function(){
        //called on destruction
        this.$().slideDown(250);
      }
    };
    

    And then in your code you apply it on your various view classes. As Ember depends on jQuery you can use pretty much any jQuery animation.

    App.IndexView = Ember.View.extend(App.CrossfadeView);
    App.PostView = Ember.View.extend(App.CrossfadeView);
    
    0 讨论(0)
  • 2020-12-29 22:58

    Billy's Billing just released an Ember module that supports animated transitions.

    0 讨论(0)
  • 2020-12-29 23:02

    I know this is pretty old, but the best solution for this context-specific animation today is probably ember liquid fire.

    It allows you to do things like this in a transition file:

    export default function(){
      this.transition(
        this.fromRoute('people.index'),
        this.toRoute('people.detail'),
        this.use('toLeft'),
        this.reverse('toRight')
      );
    };
    
    0 讨论(0)
  • 2020-12-29 23:04

    I've found another drop-in solution that implements animations in Views: ember-animate

    Example:

    App.ExampleView = Ember.View.extend({
    
        willAnimateIn : function () {
            this.$().css("opacity", 0);
        },
    
        animateIn : function (done) {
            this.$().fadeTo(500, 1, done);
        },
    
        animateOut : function (done) {
            this.$().fadeTo(500, 0, done);
        }
    }
    

    Demo: author's personal website

    0 讨论(0)
提交回复
热议问题