2013-08-16 47 views
0

我想獲得網址:本地主機#/ 234 /456分之23/ 524jk53(/ one_id/two_id/three_id/four_id)Ember.js幫助路由器地圖

Stat.Router.map(function() { 
    this.resource('one', {path: '/:one_id'}, function(){ 
      this.resource('two', {path: '/:two_id'}, function(){ 
       this.resource('three', {path: '/:three_id'}, function() { 
        this.resource('four', {path: '/:four_id'}); 
       }); 
      }); 
     }); 
}); 

和模板分離:

<script type="text/x-handlebars"> 
    {{outlet one}}<br> 
    {{outlet two}}<br> 
    {{outlet three}}<br> 
    {{outlet}} 
</script> 
<script type="text/x-handlebars" data-template-name="one"> 
    one, to {{linkTo 'two'}}two{{/linkTo}} 
</script> 
<script type="text/x-handlebars" data-template-name="two"> 
    two, to {{linkTo 'three'}}three{{/linkTo}} 
</script> 
<script type="text/x-handlebars" data-template-name="three"> 
    three, to {{linkTo 'four'}}four{{/linkTo}} 
</script> 
<script type="text/x-handlebars" data-template-name="four"> 
    four 
</script> 

路線:

App.oneRoute = Em.Route.extend({ 
    renderTemplate: function() { 
     this.render('two', {outlet: 'two'}); 
     this.render('three', {outlet: 'three'}); 
     this.render('four', {outlet: 'four'}); 
    } 
}); 

我有錯誤:斷言失敗:無法調用與 'ID' 得到一個未定義的對象上。

請幫我寫router.map這種應用的層次

+0

你想從服務器獲取這個URL?或者你想讓這個URL成爲應用程序中的一個路由? – shashin

回答

1

好了,先上去,你可以從{ path: '/:dynamic_id' }刪除/。接下來,您需要了解,當您在linkTo幫助程序中引用的路線中存在動態片段時,需要將模型(或其他)對象作爲附加參數傳遞給linkTo幫助器作爲每個動態的上下文分割。

這可能是爲什麼你會收到錯誤。你的路由器應該是這個樣子:

Stat.Router.map(function() { 
    this.resource('one', {path: ':one_id'}, function(){ 
      this.resource('two', {path: ':two_id'}, function(){ 
       this.resource('three', {path: ':three_id'}, function() { 
        this.resource('four', {path: ':four_id'}); 
       }); 
      }); 
     }); 
}); 

且模板中,你應該通過上下文段,所以例如:

{{linkTo 'two' modelOne modelTwo}}Hello World!{{/linkTo}} 

無論如何,我認爲你需要細化問一些什麼。你究竟想達到什麼目的?如果您的動態細分不是模型ID,那麼您可能需要查看覆蓋相關路線文件中serialize回調的內容。