-
Notifications
You must be signed in to change notification settings - Fork 87
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
Batch load of nested properties on a collection #75
Comments
When you talk about preloading a hasOne relation are you expecting object A to contain object B id? If that is the case then you should consider a belongsTo relation (inverse and does not imply a url dependency between objects). I think this should definitely be a core feature, I'm thinking it should work like this: var col = Model.$search().$preload('relationName', 'otherRelationName'); then a simple version of preload could be: $restmod.mixin('Preloader', function() {
'@$findMany': function(_pks) {
// not sure about how pks should be serialized.
return this.$collection().$send({ method: 'GET', url: this.url(), data: _pks }, function(_response) {
this.$unwrap(_response.data);
});
}
'@$preload': function(_relation) {
return this.$then(function() { // I think is better to chain this in the collections promise chain.
if(this.length == 0) return;
var foreignType = this[0].$type; // this is probably not the cleanest way, Im yet to implement relation metadata...
var pks = _.map(this, function(o) { return o[_relation].$pk; }); // using lodash just for simplicity.
var self = this;
return foreignType.$findMany(pks).$then(function() {
var recordMap = {};
_.each(this, function(objectB) {
recordMap[objectB.$pk] = objectB;
});
// using $decode - $encode to copy attributes, maybe it would be better to directly feed the
// request data to the nested objectB instances, that could be done by mixing the findMany
// code with this one.
_.each(self, function(objectA) {
objectA[_relation].$decode(recordMap[objectA[_relation].$pk].$encode());
});
)).$promise; // return collection promise so requests are chained.
});
}); I will add this to the 1.1 milestone. |
a |
Yes, the A object contains a B id in a property, probably named something like bId. |
How do I add this to my models? I've included it in a angular config block which is where I assumed it would go. Please help. |
The above mixin declaration is wrong, it should be: Module.factory('MyMixin', function(restmod) {
return restmod.mixin({
// mixin custom merhods here
});
}); Then to include mixin in every model use Module.config(function(restmodProvider) {
restmodProvider.rebase('MyMixin');
}); |
It looks like the preload code should use the "key" specified in the relationship, no? (Nevermind... looks like the underlying code is working some magic and setting $pk on the object even before it is loaded) |
In that code block, |
Its intended to be used in reference relations ( |
Ah, I guess I don't need support for hasOne and hasMany right now. There was just a problem with |
Should I have expected objects loaded via the findMany to use the serializers defined by |
Would be nice to be able to call .$preload('a.b') as well. Also, being able to preload arrays within objects would be nice too. |
Thanks for the fixes! I hope I can get this done soon! About the serializers, they should work, could you elaborate more on this? I agree on supporting |
The API I am working with will sometimes pre-fill relationships. For instance, the model has a "users" field, and sometimes the users field is pre-populated with users. I've noticed that if I try to use Do you have any ideas? |
This has to do with using I'm thinking on changing the |
Is there any way I can adjust the preload code you shared to construct the relationship objects correctly? This is proving to be vital for my use case. |
Thanks for working on this! Any ideas on when you might release this? |
Probably later today |
I'm wondering if it is possible to pass extra params to the preload call that will get serialized on the query string. The API I'm using supports just loading specific fields, which is sometimes desirable when preloading relationships. |
It could be added, im thinking on something like: record.$preload(
'user',
{ path: 'user.pictures', params: { include_wharever: true }, // preload with options
'user.paymentData'
); A little verbose though, would also need support from the Polulate plugin. If I have some spare time i'll take a loot at it. |
Putting this in a separate issue: #152 |
Can you give me some advice on the best way to handle this scenario?
I have an object A that has a hasOne relationship to a model of type B. Given a collection of A objects, is there a way to load in all the B's?
In my case, the API I'm calling supports a batch load, so I would assume I need to do something custom, but I'm not really sure how to accomplish it using restmod.
Thanks,
Chris
The text was updated successfully, but these errors were encountered: