2016-12-25 8 views
0

は、モデルの各ビューをDOM内の既存の各DIV(divとwidget配列を持つhaveとdiv.container)にアタッチする方法を理解できません。モデルのビューをバックボーンの既存のDIVにアタッチする

$('#preview').on('load', function() { 

var ShortcodesCollection = new V.Collections.Shortcodes(Vision.ShortcodeStorage); 

var Preview = new V.Views.Preview({ 
    collection: ShortcodesCollection, 
    el: $('.container') 
}); 

Preview.render(); 

}); 

コレクションとプレビューをレンダリングします:各モデルの

// Collection View in iframe 
V.Views.Preview = Backbone.View.extend({ 

initialize: function() { 
    this.collection.on('add', this.addOne, this); 
}, 

render: function() { 
    this.collection.each(this.addOne, this); 
    return this; 
}, 

addOne: function(ad) { 
    var shortcodeView = new V.Views.Shortcode({ model: ad }); 
    shortcodeView.render(); 
} 
}); 

ビュー:

をインラインフレームロード、コレクションにサーバからのプッシュストレージが

// Model 
V.Models.Shortcode = Backbone.Model.extend({}); 

// Shortcodes Collection Init 
V.Collections.Shortcodes = Backbone.Collection.extend({ 
    model: V.Models.Shortcode, 
}); 

// Shortcode View V.Views.Shortcode = Backbone.View.extend({ events: { 'click .widget' : 'SomeActionOnView' }, render: function(){ //console.log(this.el); //console.log(this.model.toJSON()); }, SomeActionOnView: function(){ console.log(this); } }); 

質問は、イベントをバインドする "widget"クラスを持つ各divにV.Views.Shortcodeを添付する方法です。ありがとう!

+0

divの下にあるすべてのビューをクラス「ウィジェット」で追加しますか? – Nitesh

+0

@Niteshはそれぞれの.widgetに各ビューを追加したい – 20yco

+0

すべてのビューに対して新しいdiv.widgetを作成する必要がありますか? – Nitesh

答えて

0

お試しください。

V.Views.Shortcode = Backbone.View.extend({ 
    events: { 
     'click .widget' : 'SomeActionOnView' 
    }, 

    render: function(){ 
     //code here to write stuff of this.$el 
     $("div.widget").append(this.$el); 
    }, 

    SomeActionOnView: function(){ 
     console.log(this); 
    } 
    }); 
+0

しばらくお待ちください、 – 20yco

+0

いいえ、これは何も返しません。 – 20yco

+0

これはdiv.widgetの中に現在のビューを追加するだけです。 あなたのUIにdiv.widget divがある場合、そこにあなたのビューが表示されます。 これはバックボーンビューです。現在のビューを返す場合は、最後にこれを書き出すことができます。 – Nitesh

関連する問題