2012-11-29 11 views
9

twitterのバックボーンレンダリングビューを以下のように挿入します。問題は、そのビューのコンテンツスルー・オプション・バックボーン・イベントを挿入すると起動しないことです。 $(selector).html attendanceShow.render()。elイベントが問題なく動作します。事前にありがとうバックボーンイベントとTwitterブートストラップポップオーバー

 attendance = new Attendance() 
     attendance.url = "#{attendanceUrl}/#{attendanceId}" 
     attendance.fetch 
     success: -> 
      attendanceShow = new ExamAttendanceShow({model: attendance }) 
      currentTarget.popover 
      html : true 
      content: -> 
       attendanceShow.render().el 

お礼、 ジョージ。

+1

popoverにビューを追加した後に 'delegateEvents()'を実行しようとしましたか? – deleterOfWorlds

+0

はい、試してみましたが、成功しませんでした。 – Georgi

+0

私はこの同じ問題を何時間も悩んでいます。あなたは解決策を見つけましたか?ありがとう! –

答えて

1

私が理解しているところでは、あなたのコードとあなたの説明に基づいて、あなたはポップオーバーのインスタンスを作成しているに過ぎません。私はLiveデモが動作していますが、CoffeeScript(個人的にはCoffeeScriptが嫌い)ではなく、this jsfiddleというコードを見ることができます。

data1.json

{"content": "lorem ipsum dolor sit amet"} 

index.htmlを

<div class="container"> 
    <div class="row"> 
     <button class="btn" data-target="popover">Popover</button> 
    </div> 
    <div class="row">&nbsp;</div> 
    <div class="row"> 
     <button class="btn" data-action="change-content">Change Content</button> 
    </div> 
</div> 

main.js

var Main = Backbone.View.extend({ 
    model: null, 
    item: null, 
    popover: false, 

    events: { 
     'click .btn[data-target]': 'button_click', 
     'click .btn[data-action="change-content"]': 'change_content' 
    }, 

    initialize: function() { 
     _.bindAll(this); 

     this.model = new PopoverModel(); 
     this.model.view = new PopoverContentView({model: this.model}); 

     this.item = this.$('.btn[data-target]'); 
     this.item.popover({ 
      html: true, 
      content: this.model.view.render().el 
     }); 
    }, 

    button_click: function(event) { 
     if (!this.popover) { 
      this.model.url = 'js/data1.json'; 
      this.model.fetch({ 
       success: this.model_fetched 
      }); 
     } else { 
      this.popover = false; 
     } 
    }, 

    model_fetched: function() { 
     if (!this.popover) { 
      this.item.popover('show'); 
     } else { 
      this.item.popover('hide'); 
     } 

     this.popover = !this.popover; 
    }, 

    change_content: function(event) { 
     this.model.set('content', 'Some random content... ' + parseInt(Math.random() * 10)); 
    } 
}); 

var PopoverModel = Backbone.Model.extend({ 
    defaults: { 
     content: '' 
    } 
}); 

var PopoverContentView = Backbone.View.extend({ 
    initialize: function() { 
     _.bindAll(this); 
     this.listenTo(this.model, 'change', this.render); 
    }, 

    render: function() { 
     this.$el.html(_.template('<%= content %>', this.model.toJSON())); 
     return this; 
    } 
}); 


var main = new Main({ 
    el: '.container' 
}); 
+0

私の問題は、ポップオーバーの中にビューを挿入すると、そのビューにイベントをアタッチできないということです。ありがとうございます – Georgi

+0

良い例がありますか?たぶん私はあなたを助けることができます。 –

+0

スーパーに役立つ!私の問題を解決しました。ありがとう! – streetlight

0

私はゲオルギの答えに拡大して、同様の問題を持っている、これを試してください。

ボタンを配置(動的テキストの代わりに)ポップアップ内のリンクをクリックして、イベントを処理します(例:clickイベント)。

+0

これは私のあまりにも返信ではありませんが、質問に拡張しようとしました。エリックは尋ねました... –

+0

それが動作することを確認したいですか?ポップオーバーが自動的に親に割り当てられていない古いバージョンのブートストラップ(2.0.4)がありました。 2.3.0にアップグレードすると修正されます。ごめんなさい。 –

関連する問題