0

This is more of a conceptual/architectural question than anything; the typical/popular approach to constructing and instantiating Backbone Views seems to be to only render the View AFTER successfully fetching necessary Model/Collection data from the server (in a success() or done() callback).

This is all well and good, but what if you have some sort of loading indicator or UI element within the View's template that needs to be displayed before/during the fetch? By not rendering the View until the call finishes, you effectively are unable to display such notifications to the user.

Conversely, if you render the View BEFORE making the fetch, you're now able to display such UI elements, but you now run the risk of displaying a mostly-empty template, since your Model/Collection data hasn't been retrieved yet, and this can look rather weird.

What if you need both things: UI notifications before/during the fetch, AND not to render a mostly-empty template pre-fetch? What might be some good approaches towards accomplishing this goal?

4

3 に答える 3

2

私は通常、アプリのメイン コレクションの 1 つがフェッチされていることを認識したときにローダーを表示するアプリケーション レベルのビューを持っています。直面して、私は通常、フィルターの前に ajax を実行して、常にローダーをポップし、完了時にアプリケーション全体を非表示にします。これは UI 全体をブロックしますが、明確なモジュールの問題がある、より複雑なダッシュボード タイプのアプリを実行している場合を除き、問題ありません。

于 2013-05-14T22:42:36.113 に答える
1

私はあなたが興味を持つ可能性のある解決策をブログに投稿しました ( http://davidsulc.com/blog/2013/04/01/using-jquery-promises-to-render-backbone-views-after-fetching-data/ ) . 基本的に、問題への1つのアプローチ:

  1. fetch 呼び出しの戻り値を保存します (これは jQuery promise です)
  2. ローディング ビューをレンダリングする (を使用MyApp.myRegion.show(loadingView);)
  3. 必要に応じて、この時点で (データを必要とする) ビューを既にインスタンス化できます。
  4. promise が満たされると (つまり、フェッチが完了すると)、領域内にデータ依存ビューを表示します (上記のように)。
于 2013-05-15T06:49:51.163 に答える