2
$('button').click(function(){
  App.vent.trigger("box:change");
});

App.BoxView = Backbone.View.extend({
  initialize: function(options) {

    this.listenTo( App.vent, "box:change", this.alter ); // animate, etc.
  },

  ...

});

私はメイン ビューを持っています。

  1. イベントはすべてのボックスによって処理されます
  2. 私のすべての箱はいくつかのアニメーションで完成しました

これに頭を包むことはできません(長い仕事の日)...助けてください=)より良い練習は何ですか?

私はこのようなものを探しています:

App.MainView = Backbone.View.extend({
      initialize: function(options) {

        // when all the events are complete
        this.listenTo( App.vent, "box:change" ).onComplete( this.rearrange_stuff );

        // when all the animations are complete
        this.listenTo( App.animationBuffer, "box:fadeOut" ).onComplete( this.rearrange_stuff );

      },

      ...

});

更新:アプリケーションに長いチェーンのイベント - 完了 - イベント - 完了 (ループではない) がある場合、このキューを設定するより良い方法は何ですか?

4

2 に答える 2

0

jqueryで約束を見つけました。これをすべてのビューに適用する方法を理解する必要があります..

http://api.jquery.com/promise/

(アップデート)

今のところ、私はこのようにしました... http://jsfiddle.net/Antonimo/YyxQ3/2/

App.vent = _.extend(Backbone.Events, {
    promise: function(name){
        if(typeof this._bills[name] === "undefined"){
            this._bills[name] = 0;
        } else {
            this._bills[name] ++;
        }
    },
    keepPromise: function(name){
        var that = this;
        setTimeout( function(){
            if(typeof that._checks[name] === "undefined"){
                that._checks[name] = 0;
            } else {
                that._checks[name] ++;
            }
            if(typeof that._bills[name] !== "undefined"){                    
                if( that._checks[name] >= that._bills[name] ){
                    that._checks[name] = 0; // reset
                    that._bills[name] = 0; // reset
                    that.trigger(name); // emit event
                }
            }

        }, 30);
    },
    _bills: {},
    _checks: {}
});

// usage:

alter: function() {
        App.vent.promise('some:event');
        // Animate, or not..
        App.vent.pay('some:event');
},
于 2013-02-19T08:43:00.330 に答える