data use the URL that ember is loading for the REST request

On the system I am working on right now, I have had to try to tame ember-data a bit about how it does its REST request. The way that ember-data by default figures the URL for a certain request for a model is just not gonna cut it with the backend I am using.

What I need is, to get ember-data to use the same URL that ember is loading, but with a '?json' suffix. That is, if ember switches page to my band page, and the url is /bands, I want ember-data to request /bands?json for the data it needs, not whatever it figures from the name of the model. One could say, that I wanted the URL to be calculated from the path of the loading route, instead of from the name of the model being used.

I have tried by subclassing DS.RESTAdapter{} and see if I could get the buildURL method to do this, but I can't figure out how to get the URL ember is gonna load. The buildURL method is called before ember changes the location, so I can't use document.location.href or something. I can imagine I will need a way to ask ember what it is now loading, and what the URL is.

Any ideas of how to do this?

UPDATE

There hasn't been any satisfying solutions, so I decided to just do it the dirty way. This is it:

App.RouterSignature = [
    ['index',        '/',                   '/index_models'],
    ['bands',        '/bands',              '/band_models'],
    ['band',         '/band/:band_slug',    '/band_model']
];

App.Router.map(function() {
    for (var i = 0; i < App.RouterSignature.length; i++) {
        var route = App.RouterSignature[i];
        this.resource(route[0], {path: route[1]});
    }
});

App.CustomAdapter = DS.RESTAdapter.extend({
    buildURL: function(record, suffix) {
        var url,
            suffix = '?json',
            needle = this._super(record);

        for (var i = 0; i < App.RouterSignature.length && !url; i++) {
            var route = App.RouterSignature[i];
            if (route[2] == needle)
                url = route[1];
        }

        return url + suffix;
    }
});

Now App.Routes and DS.RESTAdapter.buildURL are based off the same data. The first two values in the App.RouterSignature list is just the name of the route, the path of the route. The third value is what DS.RESTAdapter.buildURL by default guesses should be the url. My custom adapter then takes that guess, matches it with one of the items in the App.RouterSignature list and then takes the second value from that item - the routes path.

Now the requests that ember-data makes is to the same url as the routes path.


You can try to setup your Adapter like so:

 App.Adapter = DS.RESTAdapter.extend({
   ...
   buildURL: function(record, suffix){
     return this._super(record, suffix) + "?json";
   }
 });

 App.Store = DS.Store.extend({
   ...
   adapter: App.Adapter.create();
   ...
 });

See here for more info on the RESTAdapter buildURL method. Hope it helps

链接地址: http://www.djcxy.com/p/90950.html

上一篇: 与Ember的桌面集成

下一篇: 数据使用ember为REST请求加载的URL