2013-03-14 204 views
1

如何訪問子模板中的數據?將數據傳遞給子模板

例如,她是我的路由器:

App.Router.map(function() { 
    this.resource("users", function(){ 
     this.route('new'); 
    }); 
    this.resource("user", {path: '/users/:user_id'}, function(){ 
     this.route('edit'); 
    }); 
}); 

路線:

App.UsersIndexRoute = Ember.Route.extend({ 
    model: function() { 
     return App.User.find(); 
    } 
}); 

單用戶模板:

<script type="text/x-handlebars" data-template-name="user"> 
    <h2>{{name_full}} is the username on the resource template</h2> 
    {{ outlet }} 
</script> 

單用戶索引模板:

<script type="text/x-handlebars" data-template-name="user/index"> 
    <h2>{{name_full}} is the username on the index page.</h2> 
    {{#linkTo users}}Back to All Users{{/linkTo}} 
</script> 

我明白單用戶模板如何訪問用戶對象,但我如何給子路由訪問權限呢?

我已訪問Ember Router Guides,但這仍然不清楚。

回答

2

爲了給出子路由訪問,您需要定義UsersIndexRoute並從其模型鉤子返回用戶。所以:

App.UserIndexRoute = Ember.Route.extend({ 
    model: function() { 
    return this.modelFor("user"); 
    } 
}); 

modelFor將查找指定的路線,這可能是任何東西,但在這種情況下,恰好是父當前模型。

+0

有道理。謝謝。 – user1168427 2013-03-15 00:43:03