2

最初にアイテムを選択してからボタンを使用して、選択したアイテムに対して何らかのアクションを実行する代わりに、ユーザーがワンクリックを実行することをお勧めします。

動作しているように見えますが、carpool_event のイベントの後にボタンのイベントが発生する必要があるという懸念があります。これは正しくないようです。これを処理するより良い方法はありますか?ありがとう!

私のHTMLで

<template name="carpool_list">
  <h1>Carpool</h1>
  {{#each carpool_events}}
  <ul>
    {{> carpool_event}}
  </ul>
  {{else}}
    No events yet.
  {{/each}}
</template>

<template name="carpool_event">
  <div class="carpool_event">
    <span class="localDate">{{localDate}}</span>
    <span class="owner">{{owner}}</span>
    {{#if currentUser}}
      <span><input type="button" class="takeEvent" value="Take Event"/></span>
    {{/if}}
  </div>
</template>

対応するjs

Template.carpool_event.events({
    'click': function () {
      Session.set("selected_carpool_event", this._id);
    }
  });

Template.carpool_list.events({
    /**
     * Take Event Handler
     */
    'click .takeEvent': function () {
      console.log("Take event:"+Session.get("selected_carpool_event"));
    }
});
4

1 に答える 1

2

これを試すことができます:

Template.carpool_event.events({
"click": function () {
      Session.set("selected_carpool_event", this._id);
      if($(event.target).attr("class") == "takeEvent" && Meteor.userId) {
         console.log("Take event:"+this._id);
      }
    }
});

または、他の理由で両方のクリックが必要な場合、または最初のクリックをキャプチャすることを避けることができる場合は、ボタンを直接ターゲットにすることができ、ボタンでも動作するはずです (テンプレート内の任意の場所にあるボタンthis._idの同じテンプレートにハンドラーを割り当てることができます)。carpool_event

Template.carpool_event.events({
    /**
     * Take Event Handler
     */
    "click .takeEvent": function () {
       Session.set("selected_carpool_event", this._id);
       if(Meteor.userId) {
           console.log("Take event:"+this._id);
       }
    }
});
于 2013-01-25T18:58:53.750 に答える