2013-03-06 15 views
5

Ember中有一種方法可以爲特定模型配置自定義REST網址嗎?特定模型的自定義REST網址

像這個模型:

App.Post = DS.Model.extend({ 
    title: DS.attr('string'), 
    comments: DS.hasMany('App.Comment') 
}); 
App.Comment = DS.Model.extend({ 
    content: DS.attr('string'), 
    post: DS.belongsTo('App.Post') 
}); 

而且此店鋪:

app.Store = DS.Store.extend({ 
    revision : 11, 
    adapter : DS.RESTAdapter.extend({ 
     namespace : 'rest' 
    }) 
}); 

我希望的是,意見通過/rest/post/{id}/comments,而不是/rest/comments檢索這是默認的行爲。
是否可以爲一個特定的模型配置一個rest-url?

+0

我發現關於這種情況下,GitHub的問題:在post.js模型文件https://github.com/emberjs/data/issues/186 – 2013-03-06 13:10:11

回答

1

這是字面上只適用於整個應用程序的一個模型,或者這是您的REST後端使用的默認「hasMany」URI?我問,因爲我的api(django rest框架)使用這個確切的uri,並且它需要在ember-data項目上提出完整的pull請求,因爲構建適配器需要的相關「父」或「所有者」的URL所以它不存在)。

我會寫你自己的適配器(只是子類的基本適配器,所以你只覆蓋單個hasMany是不同的)。我爲我的適配器寫的方法如下,這裏是我全面的適配器供參考。

這是燼數據修訂11友好BTW(還沒有升級到12還)

https://github.com/toranb/ember-data-django-rest-adapter/blob/master/tests/adapter.js

findMany: function(store, type, ids, parent) { 
     var json = {} 
     , root = this.rootForType(type) 
     , plural = this.pluralize(root) 
     , ids = this.serializeIds(ids) 
     , url = this.buildFindManyUrlWithParent(store, type, ids, parent); 

     this.ajax(url, "GET", { 
      success: function(pre_json) { 
       json[plural] = pre_json; 
       Ember.run(this, function(){ 
        this.didFindMany(store, type, json); 
       }); 
      } 
     }); 
    }, 
2

你可以註冊一個額外的適配器和「範圍」它給你的模型。

App.Store.registerAdapter('App.Post', DS.RESTAdapter.extend({ 
    url: "/rest/post/" 
})); 
+0

直接?謝謝。 – Soullivaneuh 2015-03-20 16:32:04