Doing some development with Phoenix and really missing the conveniences and succinctness of the Rails URLHelper.
Most urgently and importantly:
Are they planned or is anybody working on them already? If not, I can take a stab at implementing them.
Please go ahead! We do need help implementing those conveniences.
Thanks, will do.
@essenciary You still working on this? If not I wouldn't mind attempting it.
After further discussions with the core team, this basically can be broken into 2 parts:
I've just started on 1 and haven't done anything on 2 yet. Don't have too much time these days so if you want to pick one up, please go ahead. Just let me know each one, so I can tackle the other.
I'm not convinced link_to_if and link_to_unless are neccessary. These are not helpers I've ever used and can be easily achieved with a wrapping <%= if expr do %><%= link "something", to: ... %><% end %>. I'll let others chime in though. Maybe these is a highly used feature by others?
@chrismccord I have to agree with that.
Not coming from the world of Rails, I had to look up what the functions did and having done that I'm now certain I'm missing something that they do as otherwise they just seem to take what would otherwise be perfectly good HTML and turn it into a function call.
For frontend developers that do not know a template system in depth it would seem your solution is the easier one for them to get to grips with, as they'd see a given anchor's CSS classes and IDs in the HTML as one would expect, not hidden in a function call.
To back this up, coming from the Python world, both of the two larger templating systems (Django's & Jinja2) stick with what you've suggested and just provide some kind of way of generating a URL and leave the rest up the user.
Having said that, if the functions do add something I'm missing, please do shout!
I would much rather see a jquery-less port of ujs included. Less overhead for a rather small set of features, not likely to conflict with other bundled assets. I'm willing to do the porting if desired.
port the rails' jquery-ujs
Can someone clarify what all is provided with the port? Are we talking about the remote: true stuff in rails, or just data-confirm?
I would much rather see a jquery-less port of ujs included.
Agreed. If we go this route, we already provide a private Ajax object in phoenix.js
link_to_if can be very useful for pagination for example <%= link_to_if @more_pages?, "next", next_page_path %> or anytime you don't want to totally remove a link, or just show that the link won't do anything (e.g. post can't be deleted if it's already been published for example). I'm not sure this is worth having a helper for, but those are some use cases.
@chrismccord The most useful ones would be data-confirm, allowing links to make post requests (for sign out, delete, etc.) and disabling submitted forms. The disabling of submitted forms is a really handy one to prevent duplicates and to show loading states on forms.
allowing links to make post requests (for sign out, delete, etc.) and disabling submitted forms
We already support this:
<td><%= link "Delete", to: post_path(@conn, :delete, post), method: :delete %></td>
link_to_if can be very useful for pagination for example <%= link_to_if @more_pages?, "next", next_page_path %>
Just to be clear, this is the same as <%= if @more_pages?, do: link "next", to: next_page_path(@conn) %> right?
I didn't realize method: :delete was supported. Awesome :+1:
link_to_if is an unclear name. link_to_if in Rails will link if the condition is true, otherwise it will print the text without a surrounding anchor tag
<%= if @more_pages? do %>
<%= link "next", to: next_page_path(@conn) %>
<% else %>
"next"
<% end %>
@paulcsmith thanks for clarifying. 6 years of doing rails and I never knew the semantics of those methods. The if/else approach is 100x more clear imo, at the small cost of possibly duplicating the link text.
No problem. Yeah it is a tricky method. It's useful if you remember what it does, but it's not immediately obvious.
In regards to the jQuery / jQuery-less ujs - initially I was thinking too that it's better to go the jQuery-less way, but after doing a bit of research, I changed my mind considering that:
In my opinion, the ideal approach would be to use rails' jQuery ujs but make it compatible with jqLite and use jQuery if already available (and skip loading jqLite all together).
I'm fully in favor of using the API that jquery-ujs expects, wherein everything is built off of data-* attributes. If done right it would still be possible to drop in another *-ujs library and have it function the same way.
Just chiming in. I strongly miss link_to_unless_current. I have used that forever as it makes doing a navbar incredibly easy. I've never used link_to_if, but I can see its use as well.
Here's a list of the UJS enhancements in question, as scraped from the jquery-ujs tests:
DELETE, POST, etc. (Already supported, at least partially)Before anyone goes porting stuff...take a look at this: https://github.com/hauleth/vanilla-ujs
@scrogson: Awesome find.
Folks, I am closing this. If someone wants to tackle ujs, it will be very welcome. I am not sure how it would fit within Phoenix, even more with the Phoenix HTML project being extracted in the future but it is definitely something people are looking for.
link_to_if and friends are also welcome additions but it is worth discussing their APIs. For example, in order to calculate the current path, we need the connection but we don't currently receive it. So we need to discuss how those new functions will look like. Please open up a separate issue for those if interested.
Thank you!