在视图之间制作动画

Jür*_*aul 2 requirejs backbone.js

在我的每个观点中,我对每个render方法都有这个:

render: function(){
    template = _.template(ViewTemplate, {foo:get});
    wrapper = this.$el;
    wrapper.is(':hidden') ? 
    wrapper.html(template).show(200) : 
    wrapper.hide(200, function(){ wrapper.html(template).show(200) });
}
Run Code Online (Sandbox Code Playgroud)

但这是如此重复,我想知道如何在我的视图之间实现动画,而不是重复相同的代码行?

McG*_*gle 5

也许只需将淡入作为实用程序方法添加到View原型中:

Backbone.View.prototype.fadeIn = function(template, wrapper) {
    wrapper.is(':hidden') ? 
    wrapper.html(template).show(200) : 
    wrapper.hide(200, function(){ wrapper.html(template).show(200) });
};
Run Code Online (Sandbox Code Playgroud)

这减少了render实现中的重复:

render: function() {
    template = _.template(ViewTemplate, {foo:get});
    this.fadeIn(template, this.$el);
}
Run Code Online (Sandbox Code Playgroud)