4

Ember data just doesn't append parameters to the query. I have a tags route like this

example

App.TagsRoute = Ember.Route.extend({
 model: function(params) {
    var tag = params.tag_name;
    var entries = this.store.find('entry', {tags: tag});
    return entries;
 }
});

but this keeps making the same request as this.store.find('entry'). i'm doing it wrong?

Edit:

My router looks like this:

App.Router.map(function(){
 this.resource('entries', function(){
  this.resource('entry', { path: '/entry/:entry_id/:entry_title' });
 });
 this.route('tags', { path: '/t/:tag_name' });
});

when i request (for example) localhost:8888/#/t/tag
the value of params.tag_name is 'tag'

edit2:

My REST adapter

App.ApplicationAdapter = DS.RESTAdapter.extend({
    bulkCommit: false,
    buildURL: function(record, suffix) {
      var s = this._super(record, suffix);
      return s + ".json";
    },
    findQuery: function(store, type, query) {
      var url = this.buildURL(type.typeKey), 
      proc = 'GET', 
      obj = { data: query },
      theFinalQuery = url + "?" + $.param(query);
      console.log(url); // this is the base url
      console.log(proc); // this is the procedure
      console.log(obj); // an object sent down to attach to the ajax request
      console.log(theFinalQuery); // what the query looks like
      // use the default rest adapter implementation
      return this._super(store, type, query);
    }
 });

edit3:

making some changes to my TagsRoute object i get the next output:

App.TagsRoute = Ember.Route.extend({
 model: function(params) {
    var tag = params.tag_name;
    var query = {tags: tag};
    console.log(query);
    var entries = this.store.find('entry', query);
    return entries;
 }
});


console output when i request localhost:8888/#/t/tag

Object {tags: "tag"}
(host url) + api/v1/entries.json
GET
Object {data: Object}
(host url) + api/v1/entries.json?tags=tag
Class {type: function, query: Object, store: Class, isLoaded: true, meta: Object…}

Ember data is attaching GET parameters. i think my error maybe is the requested url, it should be something like this
(host url) + api/v1/tags/:tag_name.json
instead of
(host url) + api/v1/entries.json?tags=:tag_name

SOLUTION

the build of ember-data (ember-data 1.0.0-beta.3-16-g2205566) was broken. When i changed the script src to builds.emberjs.com.s3.amazonaws.com/canary/daily/20131018/ember-data.js everything worked perfectly.
the proper way to add GET parameters is:

var query = {param: value};
var array = this.store.find('model', query);

thanks for your help

1
  • What's the request that's being sent?
    – Kingpin2k
    Oct 19, 2013 at 15:47

2 Answers 2

1

You are doing everything correct, are you sure the request being sent back to the server doesn't contain the query?

The full query isn't created until JQuery makes the call.

Did you look at the network tab in chrome (or whatever browser you are using) to see what it's sending back.

Watch the console in the jsbin below, it shows what happens when you use find with an object (for querying):

App.MyAdapter = DS.RESTAdapter.extend({
 findQuery: function(store, type, query) {
    var url = this.buildURL(type.typeKey), 
        proc = 'GET', 
        obj = { data: query },
        theFinalQuery = url + "?" + $.param(query);
    console.log(url); // this is the base url
    console.log(proc); // this is the procedure
    console.log(obj); // an object sent down to attach to the ajax request
    console.log(theFinalQuery); // what the query looks like
    // use the default rest adapter implementation
    return this._super(store, type, query);
  },
});

http://emberjs.jsbin.com/AyaVakE/1/edit

Additional questions:

hitting: http://localhost:8888/#/t/tag fires the tags route, sending in 'tag' to the tag_name. Your model hook is correct. Where are you seeing that the request is the same?

Additionally, you mentioned store.find('entries) and store.find('entry'). I know they handle pluralization of most things, but you should make sure those end up being the same endpoint /api/entries.

16
  • thanks for your answer @daniel ! entries and entry resources are pointing to api/entries. If i request (example) localhost:8888/#/t/tag the api filters the response with the param 'tag'. in that case i have to point to api/tags/tag.json (with param = 'tag'). The network tab in chrome is showing me the same request as this.store.find('entry'). without GET parameters attached, thats the problem. (i'm seeing that request)
    – Camilo
    Oct 19, 2013 at 18:06
  • If you add and use the adapter I showed above, and use it instead, what does it output in the console?
    – Kingpin2k
    Oct 19, 2013 at 18:16
  • What version of ember data are you using?
    – Kingpin2k
    Oct 19, 2013 at 18:17
  • let me try with your adapter. I'm using ember 1.0.0 and ember-data 1.0.0-beta.3-16-g2205566
    – Camilo
    Oct 19, 2013 at 18:23
  • i edited with the new rest adapter. the console doesnt output anything
    – Camilo
    Oct 19, 2013 at 18:31
0

The RESTAdapter sends your query object to the jQuery.ajax() method by tacking it onto the data property of the settings object. (See here for info on what is done with the settings object.)

It looks like maybe the tag name is not defined. Try to make sure that the tag_name parameter is properly coming from your route.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.