我的应用程序布局类似于附加的应用程序布局。上面板已经到页面(即在服务器的HTML响应中)。当用户与该面板中的元素进行交互时,下面动态面板的内容会相应更改。
我研究过Backbone Marionette各种View类型和Region Manager。但我仍然无法找到实现这一目标的方法。我需要从已经渲染的元素中捕获事件并相应地更改动态内容。据我了解,每次创建一个区域show
特定的木偶视图时,该区域的内容将被该视图的el
替换。因此,我不能拥有整个容器的Layout
视图。
那么使用Marionette可以做到这一点吗?
答案 0 :(得分:3)
你当然可以支持我所谓的"预渲染"或局部视图。事实上,这是我使用的Marionette View,因为我正在使用包含服务器端部分视图的应用程序:
My.PartialView = Backbone.Marionette.Layout.extend({
render: function () {
//noop
if (this.onRender) {
this.onRender();
}
return this;
},
onShow: function () {
// make sure events are ready
this.delegateEvents();
}
});
使用简单:
My.NavBar = My.PartialView.extend({
events: {
"change #search-input": "searchRequested",
"click #faq-link": "faqRequested",
"click #home-link": "homeRequested",
},
searchRequested: function (e) {
// search
},
faqRequested: function (e) {
// show the faq
},
homeRequested:function () {
// go home
}
});
var navbar = new main.views.NavBar({ el: ".my-nav" });
someRegion.show();
// or, just wire up the events manually
navbar.delegateEvents();
答案 1 :(得分:1)
我认为更好的方法是使用构造函数。 制作渲染的布局类。
App.RenderedLayout = Marionette.Layout.extend({
render: function () {
if (this.onRender) {
this.onRender();
}
return this;
},
constructor: function(){
this._ensureElement();
this.bindUIElements();
Marionette.Layout.prototype.constructor.apply(this, slice(arguments));
}
});
然后你就可以使用Marionette的全部功能了。
App.module('Some.Page', function (Mod, App, Backbone, Marionette, $, _) {
Mod.SomeLayout = App.RenderedLayout.extend({
el: '#renderedDiv',
events: {
'click .something': 'onSomethingClick'
},
regions: {
'innerRegion': '#innerRegion'
},
ui: {
something: '.something div'
},
initialize: function () {
},
onSomethingClick: function(e){
return false;
}
});
Mod.addInitializer(function(){
App.addRegions({renderedRegion: '#renderedDiv'});
Mod.someLayout = new Mod.SomeLayout();
App.renderedRegion.attachView(Mod.someLayout);
});
});