Skip to content
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

A partial added dynamically can have a valid identifications by using AJAX #468

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

vtamara
Copy link

@vtamara vtamara commented Nov 10, 2017

This is a feature that I'm using in several projects.

Besides the documentation included in this pull request,
I prepared a small application that shows how to use it
https://github.com/vtamara/cocoon-ajax-project-objective-result-indicator
and a video of the applicattion running
https://www.youtube.com/watch?v=R5lNVBVOrjU

@nathanvda
Copy link
Owner

Let me see if I understand this correctly: when clicking the link_to_add_association you do an ajax call to get an actual id (thus creating an empty child?) and then use that id instead of the randomly generated id, correct?

Some thoughts:

  • what about validations, in most cases I assume you cannot create an "empty child" and it has no use
  • what about when a user presses the link and then just abandons the form? (closes the webpage)
  • why?

The actual use of cocoon is to build a form on the client side and submit the complete form for approval.

Personally in a lot of my projects I do not use cocoon, but some watered down version that allows to add nested items and immediately saves then (without submitting a complete form), just add/remove nested items. But that is not what this is doing, afaik. So I am bit confused why you would want this.

@vtamara vtamara changed the title A partial added dinamycally can have a valid identifications by using AJAX A partial added dynamically can have a valid identifications by using AJAX Nov 11, 2017
@vtamara
Copy link
Author

vtamara commented Nov 11, 2017

Let me see if I understand this correctly: when clicking the link_to_add_association you do an ajax call to get an actual id (thus creating an empty child?) and then use that id instead of the randomly generated id, correct?

Correct.

what about validations, in most cases I assume you cannot create an "empty child" and it has no use

The method that answer the AJAX request (usually new) has to create new objects with
default information to pass the validations and/or use something like @newrecord.save(validate: false) --as the example application does.

what about when a user presses the link and then just abandons the form? (closes the webpage)

A record is created every time the user clicks the link (it is a different interaction for the user, but on the positive side it saves more frequently the actions of the user and not only at the end when the user submits).

why?

To offer more interactivity and usability in some forms, not requiring the user to submit the whole form and edit again, but making available some added elements in one region of the form in other regions that depend on them.

In the example application if the user adds one objective immediatly it can reference it from the results table (another table in the same form). I hope it is possible to see the enhanced interaction in the video or if you want to try to run the example application. Up to now, the easiest way I have found to give this interactivity to a Rails application is with cocoon and AJAX requests when creating, deleting and updating some elements and redrawing other elements that depend on them.

The actual use of cocoon is to build a form on the client side and submit the complete form for approval.

Yes I understand, I submitted this pull request in case you would like to consider that it is possible to use cocoon+jquery to build more interactive forms. I guess there are more radical approaches possibly Rails+ember or Rails+react. However for some simple cases and many projects already built with rails+jquery+cocoon I guess some interactivity could be added with AJAX and updating the right elements under the right events with jquery.

Personally in a lot of my projects I do not use cocoon, but some watered down version that allows to add nested items and immediately saves then (without submitting a complete form), just add/remove nested items. But that is not what this is doing, afaik. So I am bit confused why you would want this.

I like that. However for the scenario I'm using (that other element of the same form have available the just added item), I think once you save the nested item, you would need to update the added element in the view to use the actual identification (instead of the random one), and then update the other regions of the form that could require the added element.

I wonder if that approach would make simpler the demo application https://github.com/vtamara/cocoon-ajax-project-objective-result-indicator
Or if parts of the jquery/AJAX required to build such interactivity could be done by coocon.

@vtamara vtamara force-pushed the new_id_with_ajax branch from 9ca9540 to f710fc1 Compare May 5, 2018 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants