Question

I want to get a record from the server, but call it along with a parameter. Is there such a function that is a mixture between find('foo', 1) and findQuery('foo', 'bar=). I'm looking for a payload in the following format:

http://example.com/foo/1?bar=

update:

Here's the function I used in the end

findQuery: function(store, type, query) {
  var url = this.buildURL(type.typeKey);

  if (!Em.isEmpty(query.id)) {
    url += '/' + query.id;
    delete query.id;
  }
  return this.ajax(url, 'GET', {data:query});
}
Was it helpful?

Solution

In the case, you want to call the store as:

this.store.find('foo', {id: params.id, bar: null});

You could overwrite your adapter findQuery method.

App.ApplicationAdapter= DS.RESTAdapter.extend({

  pathForType: function(type) {
    return Ember.String.camelize(type);
  },

  findQuery: function(store, type, query) {

    var params = [];

    Object.keys(query).forEach(function (key) {
      if (key !== 'id') {

        var value = query[key],
            param = key+'=';

        if (value !== undefined && value !== null) {
          param+=encodeURIComponent(value);
        }
        params.push(param);
      }
    });
    params = params.join('&');

    var url = this.buildURL(type.typeKey);
    if (query.id) {
      url = [url, query.id].join('/');
      url = [url, params].join('?');
    } else {
      url = [url, params].join('/?');  
    }
    return this.ajax(url, 'GET');
  }


});

http://emberjs.jsbin.com/linup/2/edit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top