jQueryとMeteor
Meteor はリアクティブで、最初のページの読み込みが 1 回しかない Web Apps の世界で生まれました。jQuery は、サーバーによって生成されたページの世界で生まれました。
問題は、Meteor テンプレートで jQuery プラグインをいつ呼び出すかということです。
1) ページ読み込み時 -> 動作しません。データはまだありません。
Template.contextualFeed.feedItems = ->
Feed.find()
$("abbr.timeago").timeago() # Calling the feed right away is useless as Feed.find() is initially empty and will populate a bit later when subscription is delivering the initial data.
<template name="contextualFeed">
<ul class="feed {{isActive "feed"}}">
{{#each feedItems}}
<li class="popup-holder">
{{> contextualFeedItem}}
</li>
{{/each}}
</ul>
</template>
2) 各アイテムについて -> 動作しますが、信じられないほど無駄に思えます。
Template.contextualFeed.feedItems = ->
Feed.find()
Template.contextualFeed.jQueryMe = ->
$("abbr.timeago").timeago() # Works, but seems incredibly wasteful
<template name="contextualFeed">
<ul class="feed {{isActive "feed"}}">
{{#each feedItems}}
<li class="popup-holder">
{{> contextualFeedItem}}
{{jQueryMe}}
</li>
{{/each}}
</ul>
</template>
3) すべてのアイテムがロードされた後 -> 動作しますが、それでも本来あるべきほどエレガントではありません...
Template.contextualFeed.feedItems = ->
Feed.find()
Template.contextualFeed.jQueryMe = ->
Meteor.defer ->
#(cut) some ugly code to make sure it only executes once.
$("abbr.timeago").timeago() # Works, but seems incredibly wasteful
<template name="contextualFeed">
<ul class="feed {{isActive "feed"}}">
{{#each feedItems}}
<li class="popup-holder">
{{> contextualFeedItem}}
{{jQueryMe}}
</li>
{{/each}}
</ul>
</template>
4) データがすべてのアイテムに別々に追加されたのではなくロードされたときに発生するイベントはありませんか?
では、Meteor テンプレートで jQuery を呼び出すためのよりクリーンな方法はありますか?
PS: コード例は Coffeescript にあります...それにもかかわらず親切にしてください ;)