2012-08-01 68 views
1

在我的應用程序,我試圖保持路由結構接近的API結構成爲可能,其燼利於在基本情況下,但我仍然感到困惑的下面的情況:正確的方式使用父路由參數與燼數據?

(在參考示例http://emberjs.com/guides/outlets/#toc_nesting

什麼是最好的方式去檢索/posts/:post_id/comments數據(假設它不給我/posts/:post_id)?

我應該在comments.deserialize(...)方法中以某種方式將帖子ID傳遞給App.Comment.find(...)?有沒有比router.getPath('postController.content._id')更好的方式獲得帖子ID?我正在使用修改後的DS.RESTAdapter。

+0

當您路由到/ posts /:post_id/comments必須首先調用parent(/ posts /:post_id)的connectOutlets。可能是這可以幫助 – zaplitny 2012-08-01 21:20:26

+1

對於新的路由器的答案請參閱這裏:http://stackoverflow.com/a/15225128/1474739 – 2013-03-05 15:50:55

回答

4

父路由器參數不能作爲子路由中的參數訪問,但應該用於檢索和填充中間數據結構。

鑑於你的模型定義如下:

App.Post = DS.Model.extend({ 
    text: DS.attr('string'), 
    // ... 
    comments: DS.hasMany('App.Comment') 
}); 

App.Comment = DS.Model.extend({ 
    // You may also have: "post: DS.belongsTo('App.Post')", but we do not care for this exemple 
    text: DS.attr('string'), 
    // ... 
}); 

這應該是工作:

posts: Ember.Route.extend({ 
    route: 'posts', 

    member: Ember.Route.extend({ 
    route: '/:post_id', // [A] 

    connectOutlets: function (router, post) { 
     var applicationController = router.get('applicationController'); 
     applicationController.connectOutlet('post', post); // [B] 
    }, 

    show: Ember.Route.extend({ 
     route: '/' 
    }), 

    comments: Ember.Route.extend({ 
     route: 'comments', 

     connectOutlets: function (router) { 
     var postController = router.get('postController'), 
      comments = postController.get('comments'); // [C] 
     postController.connectOutlet('comments', comments); 
     }, 
    }), 
    }) 
}) 
  • [A]:樁模型實例將自動地通過路由器檢索,根據慣例:post_id是指Post模型實例與給定的id(見this comment)。
  • [B]:這裏,PostController將由路由器通過上下文填充:post,這是Post實例上檢索(見[A])。
  • [C]:PostControllerObjectController(即Proxy),因此它直接保留註釋。
+0

我理解這根據路由位(並閱讀本文後,好一點) ,但我仍然需要來自API端點/帖子/ [post_id] /評論的評論數據。在這個API的情況下,它不會附帶/ posts/[post_id]響應 - 只有一個URL而不是數據。 DS.hasMany(沒有嵌入標誌)究竟幹什麼?謝謝您的幫助。 – dechov 2012-08-02 15:52:11