2013-06-05 25 views
0

在我的index.html中,我有空的<body>標記。我有一個視圖加載視圖的內容(通過icanhaz)。我想有一個全球區域(綁定到body標籤),並通過視圖在其中放置一些內容。我遇到的問題是它會將全新的身體標記放入現有的身體標記中。backbone.js/marionette.js全球地區:身體內部身體

這是應用程序:

var Application = new Marionette.Application(); 

Application.addRegions({ 
    bodyRegion: new Marionette.Region({el: "body"}) 
}); 

Application.addInitializer(function(options) { 
    Application.bodyRegion.show(new RootView()); 
}); 

這是我rootView:

Backbone.View.extend({ 
    tagName: 'body', 

    initialize: function(options) { 
     logger.init('root'); 
     loader.loadTemplate(template); 
     this.headerView = new HeaderView(); 
     this.usersView = new UsersView(); 
    }, 

    render: function() { 
     logger.render('root'); 
     this.$el.html(ich.rootTemplate); 
     this.headerView.setElement(this.$el.find('#header')).render(); 
     this.usersView.setElement(this.$el.find('#main')).render(); 
     return this; 
    } 
}); 

這是我root.ich:

<script id="rootTemplate" type="text/html"> 
    <div id="header"></div> 
    <div class="container" id="main"></div> 
</script> 

我有問題是,後渲染我已經在另一個標籤裏面有一個標籤。如果我不使用木偶和使用純骨幹觀點相反,具有以下幾行:

var rootView = new RootView(); 
rootView.setElement('body').render(); 

一切工作正常。我究竟做錯了什麼?

回答

0

tagName: 'body',創建一個新的body元素(您的第二個元素)。如果您的元素必須是主體,則使用el: 'body',則tagName用於爲沒有該元素的視圖創建新元素。

+0

然後我從'Application.bodyRegion.show(new RootView())'行中得到'未捕獲錯誤:HierarchyRequestError:DOM異常3';' – ducin

+0

Dunno Marionnette,我想你的視圖元素不能是該區域的元素? – Loamhoof

+0

沒有,似乎並不是那樣,但你肯定有一些DOM插入錯誤。 – Loamhoof

0

只是刪除標記名聲明,你應該沒問題。 你的視圖根視圖將插入到身體(你的區域el)創建一個div本身,這將是它的EL。

如果一個div是不能接受的,你需要的區域EL是那麼身體 改變你的區域聲明

Application.addRegions({ 
    bodyRegion:"body" // this will create your new region just fine with body as its el 
}); 

和你的觀點應該是這樣的......

Backbone.View.extend({ 
el: 'body', // with this line your view will be atached to an existing element in this case the body. 


initialize: function(options) { 
    logger.init('root'); 
    loader.loadTemplate(template); 
    this.headerView = new HeaderView(); 
    this.usersView = new UsersView(); 
}, 

render: function() { 
    logger.render('root'); 
    this.$el.html(ich.rootTemplate); 
    this.headerView.setElement(this.$el.find('#header')).render(); 
    this.usersView.setElement(this.$el.find('#main')).render(); 
    return this; 
} 

});