-
-
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
Polymorphism doesnt currently work with store.find. #1766
Comments
The original polymorphic feature was tied to associations because |
I have a scenario as follows (contrived):
I'd prefer A long time ago, you were able to do this with https://coderwall.com/p/hi6hya method - but that code is long gone :) The closest implementation I've seen recently would be #1845 @kylenathan Is this functionality similar to what you're looking for? @hjdivad I was close to getting this to work, but I ran into the problem that the object is constructed before receiving the payload. If it's no longer necessary, do you know if there any plans on removing that functionality before 1.0? |
we should stop eager constructing these objects, as that causes other issues. Someone just needs to sit down and do the refactor. |
yeah switching from types to strings was a great move; this is one of the loose ends that still needs to be tied up |
@stefanpenner Looking at the code, it seems like the create part of the find-or-create-esque functionality of Does it sound like I'm on the right track? Basically passing along the Also curious if this conflicts with the |
@stefanpenner possible to get some pointers on how to refactor this? Keen to get it working. Worth doing or is ssot branch going to change everything? |
@igorT feel free to correct me if I'm wrong but @kylenathan I don't think SSOT, at least at a high level, has any impact on this. I say "at a high level" b/c SSOT has some other misc changes to the store, but its focus sidesteps this. The main issue API-wise is that to support polymorphism in find you must delay creating the object until you have the payload (as you will not know the concrete type until then). So the main refactoring task is to look for references to The above is a bit hand-wavy. For instance, |
@hjdivad great, thanks. Digging into ED internals and will start having a play around. |
// polymorphic-find.js
import Ember from 'ember';
import ajax from 'ic-ajax';
export default function(url, id){
var store = this.store;
if (id){
url = url + '/' + id;
}
return ajax(url).then(function(data){
var barsPayload = {};
data.bars.forEach(function(bar){
var barType = Ember.String.dasherize(bar.links.bar_type);
var typeArray = barsPayload[barType];
if (!typeArray){
typeArray = barsPayload[barType] = [];
}
typeArray.push(bar);
});
store.pushPayload(barsPayload);
var bars = [];
data.bars.forEach(function(bar){
var id = bar.id;
var type = Ember.String.dasherize(bar.links.bar_type);
bars.push(store.getById(type, id));
});
if (id){
return bars[0];
}else{
return bars;
}
});
}; On a project we are simulating the effect of polymorphic find with $.ajax. Maybe this will be helpful to someone. This breaks the payload up based on model types, pushes that into store (the cc @krisselden |
Anyone currently looking at this? It'd be a really useful addition. Will have to go with the solution @denisnazarov provided (thanks!), but it's not as clean as having Ember Data do it. |
Agreed, just ran into this issue as well. |
@igorT Is this something we want to see implemented ? |
This will soon be possible to implement with #2991 |
I think this is implemented now, closing. |
Not yet implemented but waiting on an RFC - emberjs/rfcs#67 |
Given the above models, the following returns models of the type App.Story regardless of the server response.
From what I can see this is because when defining polymorphic model relationships we have the {polymorphic: true} flag, but no such option exists for store.find (AFAIK).
I've hacked a quick, dirty fix that works by checking the data for the presence of a 'type' key and adjusting the type arg to recordForId accordingly, but that's not ideal. Should this be checking for a polymorphic flag in the args to store.find? Any other alternatives?
The text was updated successfully, but these errors were encountered: