2

コードは Addy Osmani: Learning JavaScript Design Patternsからのもので、実際には実装できませんでした。

私はここで目が見えないかもしれませんが、これの何が問題なのかわかりません:

ReferenceError: 変数が見つかりません: items

アンダースコア テンプレート:

<script id="resultTemplate" type="text/html">
    <% _.each(items, function( item ){  %>
            <li><p><img src="<%= item %>"/></p></li>
    <% });%>
</script>

jQuery:

var resultTemplate = _.template($("#resultTemplate").html());

[...]

デモ: jsFiddle

4

2 に答える 2

0

これは機能します:

// Subscribe to the new results topic
$.subscribe( "/search/resultSet" , function( e, results ){

    $( "#searchResults" ).append(resultTemplate( {items: results} ));

});

_.template()渡したオブジェクトを分解します。したがって、結果オブジェクトを渡す代わりに、結果で構成される項目オブジェクトを含むオブジェクトを渡します。テンプレートはこれを展開し、アイテムを反復処理できます。

参照: http://jsfiddle.net/nmBGC/2/

于 2013-07-16T16:19:15.863 に答える