I often find myslef in the need to split one action into multiple different ones, mostly for index actions. Here’s an example, we have a /posts resource, where each post has multiple attributes. We might want to do
-
/posts- list of posts -
/posts?query=foofulltext search -
/posts?tags=foo,bar,baz- search by tags -
/posts?random- random post -
/posts?random&limit=3- 3 random posts -
/posts?featured- featured posts -
/posts?recommended- recommended posts for the current user
Now this is just the begining of a long list, and of course these can mix. In ideal world, these could be just be URLs, for example we could get published posts at /posts/published, but that’s not possible to do with Ember Data.
The result is that the backend looks like this
def index
if params[:query]
render json: Post.all
elsif params[:tags]
...
elsif ...
# and the list goes on
end
Of course I would make a custom router that would dispatch based on the params, but that doesn’t feel RESTful at all. It kinda reminds me of the old PHP days of /index.php?action=foo¶ms=bar.
If we do decide to use custom routes, it forces us to go with store.load and $.ajax, for example
$.getJSON("/posts/published").then(function(data) {
this.get("store").load(App.Post, data.post);
}.bind(this));
but what if we’re sideloading in this case? We basically need to do that manually.
$.getJSON("/posts/published").then(function(data) {
var store = this.get("store");
store.load(App.Post, data.post);
store.load(App.Comment, data.comments);
}.bind(this));
Am I the only one experiencing this? I found that if the app is small-medium size, it isn’t such a problem to stick everything into one action, but once it grows and you have a lot of different ways of using the same resource, this approach becomes completely unmanagable.