Closed chiefGui closed 9 years ago
Guys, seems like Travis is having some difficulty to set up the SDK in their environment. I really don't want to touch this because I am not sure if there is an appropriate approach to make it work since this is a commercial tool.
May I ask you to take a look whether my PR get accepted?
Hey guys!
The purpose of this PR is giving to the
.or()
method of the Query entity the capability to accept anarray
as the first argument.But, why?
Let's suppose you have a custom object called
posts
and want to retrieve their owner's data—how do you do that?Given the following scenario:
You have two options:
fetch()
, just{ populate: true }
.But, what is the problem of this solution? Basically, iterating over each post will result in a new request to getting its author's data. Having 10 posts, 10 pointless requests will be made just to retrieve a few amount of data.
See? 3.5 seconds at
localhost
to retrieve 6 authors. Can you imagine how this could become a riot just by retrieving more results? Also, it can be expensive for the company and for your clients, since the plans are mainly based in requests number.Finally, the solution I'm proposing here is enabling the injection of an array as an
.or()
method argument. Actually, its very first—and unique—argument.Talking to Claudio, he reached this syntax:
But this way we were passing explicitly the ids of the users—in the end, it won't be useful enough because the data I want to retrieve is dynamic; recursive. By then, him and me were mining a little more a polished solution and we get almost there:
Now the challenge was: "How to pass recursively the
query
as different properties to the.or()
method?"Basically, the answer is injecting recursively the representation of the objects—their variable names—as properties, something like this. Yes, it would work, but the complexity doesn't worth.
So, that's why I created this PR: using this
1.2.2
version I'm proposing, this became possible:Also, instead of having one request for each
post
, you'll have just one more request. After receiving the response, all you need is to set the way you want the users accordingly to their posts. In my case, I used underscore.This is the result:
Two requests only:
posts
(in screenshot's case,testimonals
);Programatically speaking, this is not a technical workaround. Actually, just an "hipster" way to solve a problem. While the
{ populate: true }
for theUser
object doesn't come, I think this is a good way to go—logical and not hard to implement.If you wanna test before merge and without hassle, just link this file to a
<script>
tag instead of using the conventional version of the Stamplay SDK.Finally, I want to highlight that this is totally backward compatible. I didn't touch the current code—as you can see at https://github.com/Stamplay/stamplay-js-sdk/commit/9b2b18733c3961077741fffe0a16285f58d5c81d —, and that's why this update is launching like as a patch instead of a major or minor version: no need for this.