バックボーン ビューを作成しましA
たB
。
var App = {
run: function () {
this.aview = new aView();
this.bview = new bView();
}
};
// First View
var aView = Backbone.View.extend({
el: '#a',
render: function () {
this.$el.html('foobar A');
}
});
// Second View
var bView = Backbone.View.extend({
el: '#b',
render: function () {
this.$el.html('foobar B');
// want to put `foobar A` content into #c which is inside #b
// but aView's el lets it be into #a.
// One Possible solution would be to create a new instance of aView
// and change its `el`
var tempaView = new aView({ el: '#c' });
tempaView.render(); // Now it puts `foobar A` into #c
// Or another solution would be to pass a new element as a param to
// App.aview.render('#c'); and check whether param is undefined, otherwise
// the content will be added into #param (in this case #c).
}
});
App.aview.render(); // Puts `foobar A` into <body>
App.bview.render(); // Puts `foobar B` into #b and `foobar A` into #c
だから、私の質問は、どちらが適切な方法ですか? これとは別に、誰かがより良い解決策を持っていますか?