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 に答える