Backbone: コレクションとモデルのフェッチ/保存などのステータス

作成日 2013年01月24日  ·  9コメント  ·  ソース: jashkenas/backbone

コレクションにデータが入力されているのか、それともデータが入力されているのかを知るには、次のようなものがあります。


collection.fetch();
collection.isFetching()===true; // display loading 

(collection.isFetching()===false; && collection.length) // display no records found

実際のユースケース(単純化、オブジェクトはシステムの奥深くに渡されます):

javascript() // somewhere in the code collection = new MyCollection(); // somewhere else in the code view = new MyView({collection:collection}); // and somewhere else in the code collection.fetch();

ビューは収集シグナルをリッスンし、ロード中/レコードなし/レコードリストのいずれかを表示します

モデルの場合、保存/削除/更新することもできます

question

最も参考になるコメント

@ braddunbar@ caseywebdevアイデアは、これら2つの部分を分離することです。 (ビューは、_ 'clean'_コレクションだけでなく、任意の状態でコレクションを取得できる必要があります)
シグナルを使用してこれを実行できることは承知していますが、多くの場合、この方法では実行できません。 アクションの順序が重要です

ビューが「request」および「sync」シグナルをリッスンする必要があることに同意しますが、コレクションがビュー

ビューが任意のコレクションを取得する例を考えてみましょう。 このコレクションは、ビューに渡されるときに_fetched _、_ fetching_、または_unfetched_のいずれかになります(tl:dr例3を参照)

例1(信号が機能する場所)

collection = new Backbone.Collection();
view = new Backbone.View({collection:collection});
view.render();
// what should the view render ?
collection.fetch() // now sync signal gets sent

例2(ビューはすでにフェッチされていますが、空のコレクション、シグナルも機能します)

collection = new Backbone.Collection();
collection.fetch();  // now sync signal gets sent
// after fetch completes
// view won't be able to receive 'sync' signal
view = new Backbone.View({collection:collection}); 
view.render(); 
// at this point collection.length == 0. 
// I guess the view can listen to 'sync' and then render 'empty' 

例3(最も重要なのは、信号が機能しないことです)

collection = new Backbone.Collection();
collection.fetch();  // now 'request' signal gets sent, but it is pending
// view won't be able to receive 'request' signal
view = new Backbone.View({collection:collection}); // at this point collection.length == 0
// view did not receive the 'request' signal, therefore has no idea that the collection is fetching
// and the view should render  'loading'
// after 2 seconds, 'sync' signal gets sent

ビューは、collection.length == 0の場合、フェッチされないと想定してはなりません。
ビューは、フェッチされていない状態(またはその他の状態)でコレクションが与えられていると想定してはなりません。

コレクションをビューから切り離す場合、ビューはコレクションの状態を認識できる必要があります。

同じことがモデルにも当てはまります

全てのコメント9件

こんにちは@ g00fy-! この目的には、 "request" "sync"イベントと

collection.on('request', function() {
  // loading...
}).on('sync', function() {
  // display records or "none found"
});
collection.fetch();

_編集- "fetch"ではなく、 "request"イベントを意味しました。_

それはうねりのアイデア@braddunbarだと思います!

ありがとう@caseywebdev! :)

それがうまくいかない場合は@ g00fy-までお知らせください。

@ braddunbar@ caseywebdevアイデアは、これら2つの部分を分離することです。 (ビューは、_ 'clean'_コレクションだけでなく、任意の状態でコレクションを取得できる必要があります)
シグナルを使用してこれを実行できることは承知していますが、多くの場合、この方法では実行できません。 アクションの順序が重要です

ビューが「request」および「sync」シグナルをリッスンする必要があることに同意しますが、コレクションがビュー

ビューが任意のコレクションを取得する例を考えてみましょう。 このコレクションは、ビューに渡されるときに_fetched _、_ fetching_、または_unfetched_のいずれかになります(tl:dr例3を参照)

例1(信号が機能する場所)

collection = new Backbone.Collection();
view = new Backbone.View({collection:collection});
view.render();
// what should the view render ?
collection.fetch() // now sync signal gets sent

例2(ビューはすでにフェッチされていますが、空のコレクション、シグナルも機能します)

collection = new Backbone.Collection();
collection.fetch();  // now sync signal gets sent
// after fetch completes
// view won't be able to receive 'sync' signal
view = new Backbone.View({collection:collection}); 
view.render(); 
// at this point collection.length == 0. 
// I guess the view can listen to 'sync' and then render 'empty' 

例3(最も重要なのは、信号が機能しないことです)

collection = new Backbone.Collection();
collection.fetch();  // now 'request' signal gets sent, but it is pending
// view won't be able to receive 'request' signal
view = new Backbone.View({collection:collection}); // at this point collection.length == 0
// view did not receive the 'request' signal, therefore has no idea that the collection is fetching
// and the view should render  'loading'
// after 2 seconds, 'sync' signal gets sent

ビューは、collection.length == 0の場合、フェッチされないと想定してはなりません。
ビューは、フェッチされていない状態(またはその他の状態)でコレクションが与えられていると想定してはなりません。

コレクションをビューから切り離す場合、ビューはコレクションの状態を認識できる必要があります。

同じことがモデルにも当てはまります

@ g00fyビューに送信した後、コレクションをfetchだけにできない場合は、コレクションにfetchingフラグなどを設定します。

collection.on({
  request: function () { this.fetching = true; },
  'sync error': function () { this.fetching = false; }
});

これで、ビューはthis.collection.fetchingアクセスでき、 this.collectionsyncまたはerrorイベントを発生させるのを待つこともできます。

@caseywebdev
リクエストが中止されるか、2つのリクエストが同時に送信される可能性があります。

// example 1
collection.fetch().abort()  // and collection.fetching == true
// example 2 - don't care about abort();
collection.fetch();
collection.fetch();
// 1st request compleated
!!collection.fetching == false // but the 2nd request is pending - should it be loading or not?
// 2nd request completed

これに対する解決策は、do deferredxhrを接続することです。

collection.on({
    'request':function(model,xhr){ 
        this.fetching = true; 
        xhr.fail(function(){this.fetching = false }.bind(this));
    },
    'sync error': function () { this.fetching = false; }
});

そのため、これを組み込むと便利かもしれませんが、一方で、このようなすべての機能強化が完了すると、BBコードベースが肥大化してしまいます。

このようなすべての機能強化が成功した場合、BBコードベースは肥大化するでしょう。

アプローチよりも膨満感ではないと思います。 同時リクエストを処理する方法はたくさんあります。 バックボーンは、それらを処理するために必要なプリミティブを提供し、邪魔にならないようにします。

@ g00fy-目的に合わせて有限状態マシンを作成する方法を検討することをお勧めします。 Chaplin.jsには、複製が非常に簡単なはずのものがあります。また、特定のユースケースに適したifandelse / machina.jsブログ投稿を参照)もあります。

このページは役に立ちましたか?
0 / 5 - 0 評価