2013-05-05 71 views
4

我的API的一部分處理API端點的設置如下:灰燼JS嵌套資源URL

/v1/place/:place_uuid - 檢索一個地方

/v1/place/:place_uuid/cart - 檢索與該地方

相關的購物車對象我的路線設置正確(見下文),但由Ember製作的API請求與我自己的不匹配。

App.Router.map(function() { 
    this.resource('place', { path: '/:place_uuid' }, function() { 
     this.resource('category', { path: '/:category_uuid' }, function() { 
      this.resource('product', { path: '/:product_uuid' }); 
     }); 
     this.resource('cart', { path: '/cart' }); 
    }); 
}); 

當我在瀏覽器中加載/:place_uuid我看到的API請求/v1/place/:place_uuid這是正確的。路線與返回的對象一起呈現。

當我在瀏覽器中加載/:place_uuid/cart時,我看到了與上面相同的內容,但也有第二個API請求/v1/carts。除了/v1/carts URL之外,這一切都很好。我需要它/v1/place/:place_uuid/cart

我試圖使用buildURL與車型自定義適配器..但沒有運氣。我沒有訪問place_uuid低於任何地方,所以我不能把它注射。

cartAdapter = App.Adapter.extend({ 
    buildURL: function(record, suffix) { 
    console.log(record); // just a string. 
    var url = [this.url]; 
    url.push('places'); 
    url.push(place_uuid); // if only I had access to place_uuid I could inject it here 
    url.push('cart'); 
    return url.join("/"); 
    } 
}); 
App.Store.registerAdapter('App.Cart', cartAdapter); 

這將是巨大的,如果我可以用我的原API終點,我想另一種選擇是改變端點/v1/carts?place_uuid=:place_uuid(Ember似乎更喜歡,但有點棘手,因爲我不在後臺工作)。

任何意見表示讚賞。

回答

4

Ember還不支持嵌套的REST URLS /資源。有一個open issue for this functionality,你可以監控。

+2

這似乎是一個問題依然。我很樂意忽視這個世界,假裝這些API不存在 - 但它們確實存在。那麼,我唯一的解決方案是切換框架,還是有推薦的黑客來規避行爲? – shredding 2014-09-13 06:37:49

1

這裏是我的方法:

修補DS.Store這樣的:

import DS from 'ember-data'; 

export default { 
    name: 'patches', 

    initialize: function() { 
     // +++ Nested routes + 
     DS.Store.reopen({ 
      __nestedAdapter__: null, 

      nest: function(type) { 
       this.set('__nestedType__', this.adapterFor(type)); 
       return this; 
      }, 

      withResources: function(resources) { 
       this.get('__nestedType__').set('nestedResources', resources); 
       this.set('__nestedType__', null); 
       return this; 
      } 
     }); 
    } 
}; 

添加到您的適配器:

import DS from 'ember-data'; 

export default DS.RESTAdapter.extend({ 

    nestedResources: {}, 

    buildURL: function(type, id, record) { 
     var resources = this.nestedResources; 
     var parts = [this.get('host')]; 

     for(var key in resources) { 
      if (resources.hasOwnProperty(key)) { 
       var resource = resources[key]; 
       parts.push(
        Ember.Inflector.inflector.pluralize(key) 
       ); 
       if(isNaN(resource)) { 
        parts.push(resource.get('id')); 
       } else { 
        parts.push(resource) 
       } 
      } 
     } 

     parts.push(
      Ember.Inflector.inflector.pluralize(type), 
      id 
     ); 

     this.set('nestedResources', {}); 
     console.log(parts.join('/')); 
     return parts.join('/'); 
    } 

}); 

使用一個表達語法:

that.store.nest('user').withResources({comment: comment}).find('user') 

這可以b呃確實縮短了,但我喜歡那樣。

1

無恥插頭:)

我寫https://github.com/amiel/ember-data-url-templates幫助只是這種情況。它適用於更新版本的ember-data。有了它,你可以做這樣的事情:

App.CartAdapter = App.ApplicationAdapter.extend({ 
    queryUrlTemplate: "/v1/place/{placeId}/cart", 

    urlSegments: { 
    placeId(type, id, snapshot, query) { 
     return query.placeId; 
    } 
    } 
}); 


// in the route 
this.store.query('cart', { placeId: placeId }); 

我希望幫助:)