2

在我的每一个观点上,我对我的每一种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) });
}

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

4

1 回答 1

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) });
};

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

render: function() {
    template = _.template(ViewTemplate, {foo:get});
    this.fadeIn(template, this.$el);
}
于 2012-12-23T18:50:33.693 回答