各項目を表示するためにテンプレートを読み込む必要があるクライアント側の JS アプリがあります。それらがメモだとしましょう。
問題は非同期部分にあります。テンプレートが一度だけ読み込まれるようにすることはできません。メモが関数を呼び出すたびにロードされますrender
。
ここにいくつかのコードがあります:
var notes = [ {}, {}, {} ] // Some Note objects
notes.forEach( function( note ) {
render( note )
}
// Have some variable to hold the template
var template
// Now on the render function
function render( note ) {
// First, if the template exists, go straight to the next function
if ( template ) {
// The display function takes care of appending the note
// to the body after applying datas on the template
display( note )
}
else {
// loadTemplate just loads the template in an ajax request
// and executes the callback with the template as argument
loadTemplate( function( data ) {
// I fill in the template variable
template = data
// And I display the note since the template is available
display( note )
} )
}
}
したがって、この場合、これを防ぐためのチェックがあっても、テンプレートの 3 倍の負荷がかかります。これは、3 つのテンプレートがそのまま他のテンプレートに移動するためだと思いますが、どうすればこれを防ぐことができますか?
ブラウザーがフリーズするため、sync ajax loading を使用したくありません。
編集:最後に、@ Managuのソリューションを少し変更して使用しました。
彼のループを使用する代わりに、よりエレガントな次のループを使用しました。
while ( backlog.length ) {
display( backlog.shift() )
}