-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to use query string params with store.find() #1576
Comments
It sounds like this warrants a new feature, and this is definitely hokey, but you could do something like this in the meantime:
I'm sure this is precisely the sort of thing you were trying to avoid since it conflates the purpose of your Index endpoint, but you could implement this today. |
To be clear, based on the description of |
@mlb5000 thanks for the reply. It looks like its going to need a new function in the store and adaptor. Fortunately our system is a few months off any sort of release yet. I will give it a go next week and submit a PR if everyone thinks its something that should be in the master. |
I like the idea myself, but have only just started to get involved. I'm sure some of the veterans will have an opinion. |
To play devil's advocate, why not just sideload related entities all the time? Costly query? |
JSON API recently merged a spec[1] describing the ability for a client to request related documents from a compliant server. I think that supplying arguments to store's methods would be a reasonable way to expose this functionality to users of a hypothetical JSON API adapter. |
I feel like this is really important. For example I'm need to generate additional response data for an |
I really think abuiles PR should be reconsidered; this is just one of those things that /should work/. |
I'm using Ember 1.5 for one o my projects and have the same need that is detailed out in this issue. Interestingly enough tho...the way around this was to do the following:
This would produce the correct URL and also maintain the data binding. Hopefully this helps anyone else running in to the same or similar issue. |
Perhaps not clear why passing query params would be supported when fetching a collection but not a single item. |
I went out to try to at least try and write a new function that wrapped findQuery and extracted a single element. I did not succeed (payload gives me content, but there's problems extracting it). This was I guess because as an end user I know what to do with Promises (Ember is quite transparant with them), but I don't have any traction yet with implementing them from my side. I ended up just writing {#each} in my .hbs, knowing that it will only get executed once. (in Coffeescript, sorry for that)
to be used as
|
Thanks for the comment @morficus, however, the following attempts at passing query parameters when fetching a single item failed:
this.store.find('post', {'include': 'comments', 'id': 1});
this.store.filter('post', {'include': 'comments', 'id': 1});
Both resulted in:
http:////posts?include=comments&id=1
As opposed to:
http:////posts/1?include=comments
Any suggestions? |
As some have noticed, there is no way to customize Serializers and Adapters to accomplish a query for a single record because the contract between the Store, Serializer, and Adapter won't allow it. Because of this, the store has to get involved too, and the store is not supposed to be customized. :( Here is a hack that I just put into place to accomplish this. Of course this uses non-public APIs so good luck during upgrades. First I have a file that exports a function that I call
This hits an API like You could use that as a stand-alone function, but I went one step further and added it to my store in an initializer:
I'm a little worried about the |
Hello, @wycats are there any news about the progress. We would like to use this feature to change the language of an items properties. It would be really great if we could find a possible solution. |
👍 @mitchlloyd very nice work on that work around. It works fantastic. I do believe this is a very important case to support in ember data. I am currently following the JSON standards proposed at http://jsonapi.org/ and this query case is too common to not be supported. |
I took the work around of @mitchlloyd above and did it in straight JS. Config: DS.Store.reopen({
findOneQuery: function(type, id, query) {
var store = this;
var adapter = store.adapterFor(type);
var serializer = store.serializerFor(type);
var typeClass = store.modelFor(type);
var url = adapter.buildURL(type, id);
var ajaxPromise = adapter.ajax(url, 'GET', { data: query });
return ajaxPromise.then(function(rawPayload) {
var extractedPayload = serializer.extract(store, typeClass, rawPayload, id, 'find');
return store.push(typeClass, extractedPayload);
});
}
}); Usage: this.store.findOneQuery('model', params.id, {'include': 'other_models'}) Worked like a charm for me. I would really like to see something like this included with Ember Data. |
@shawndeprey I fixed your code so it works for model specific adapters:
Thanks for pointing me in the right direction :) |
Thanks for the patches hope the pull request is accepted |
Awesome, It worked perfect!!!! |
You will now be able to use |
When are you targeting this feature to be release? |
|
Is this the right way to use it? this.store.queryRecord('team', 77, {include_members: true}); Doesn't seem to work for me |
@jpoiri try |
@wecc I'm getting this now using the RestAdapter |
thanks, I guess I'll have to wait |
@jpoiri did you solve the issue? For me the provided solution does not work as I would expect it. this.store.queryRecord('team', 77, { query: { include_members: true } }); I use ember-data 1.13.7 with the RESTAdapter and ember 1.13.6 What I did to get it working as expected (or as I would expect it): // SOMEWHERE IN A CONTROLLER OR ELSEWHERE
this.get('store').queryRecord(this.get('modelname'), {
__id__: id,
contentLanguage: newLang
}).then(function (response) {
// HANDLE SUCCESS
}, function (error) {
// HANDLE ERROR
}); And then I need to adapt buildURL in the RESTAdapter: import Ember from 'ember';
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
//...
buildURL: function (type, id, snapshot, queryType, query) {
if (queryType === 'queryRecord' && query.__id__) {
url += '/' + query.__id__;
query.__id__ = null;
delete query.__id__;
}
return url;
}
}); I think this is a hack and not a nice software design. Is Could someone help me figure out what would be a nice solution? Thanks a lot :-) If someone is interested; our use case is the following: |
@tschoartschi check out @wecc's example here. #3596 (comment) it worked for me :) |
@wecc 's example is excellent. Thanks! Incidentally, I was able to call the querystring object in BuildURL itself also:
I called the report model from the controller like this:
where Bryan |
Struggling a bit with
|
I've come across an issue where I need to append query string params to a find request.
So basically I need to do something like this:
Which should produce a GET request like:
/contacts/1?embed=company
Going through the code I couldn't see a way of doing this. Has anyone got any thoughts or ideas on how I might achieve this?
The text was updated successfully, but these errors were encountered: