-
Notifications
You must be signed in to change notification settings - Fork 8
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
Updated javascript_initialization to take in options to define custom action name #15
Conversation
Hi @yzdong! Thanks for the PR. May I understand how you would use this? Typically, Do you call Am also thinking then if it makes sense to include another level after |
I'm calling javascript_initialization custom_name: @template_name, where @template_name is provided in the custom view template as: Mm yah, that could be an option too. Not quite sure how you would get the view name thought; is that available in the controller object? |
Just checked. Not really. Have to do more work to get it out.
Are you calling this in the Might want to use Am stll wondering if you really require that granularity of breaking out JS by views. |
Mm yah, I am calling javascript_initialization in layouts/application.html.erb. Guarding against an empty @template_name does make sense, but if there is no @template_name then it defaults back to the default controller action. Heh yeah unfortunately with our current setup the JS does break if it's just action_name... I guess the tradeoff is in flexibility v. complexity... The default setup works with a standard Railsy CRUD setup but not quite if are different paths from one controller aciton. |
This is what I think would be better in terms of "API": In the helper
after the
Then to use it, all you have to do in your view is to add:
Does this make sense? wdyt? |
Oh, so Other than that, it makes sense to name the parameter through |
I would actually keep both. So the standard is:
|
I see, yah I guess it makes sense in reducing code duplication if you share code that's common to init controller#action. I see you've worked on refactoring some stuff-- I'll pull the newest master, write some tests up for this branch, and update the pull request. |
…rate an additional Application_name.controller_name.init_js_init_method javascript method
Added the changes, let me know your thoughts! |
let(:action_name) {"update"} | ||
|
||
it "uses the custom js init method" do | ||
view.javascript_initialization(js_init_method: 'custom').must_match "Dummy.#{controller_name}.init_custom();" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something went wrong with the indentation, could you check please, thanks!
Hi Juanito, I've fixed the indentation issues and changed the if statement, let me know if it works! |
Hey Winston! Modified javascript_initialization so that we can pass it an optional value to specify which action name it should use.
The use case is that one controller action could be rendering two different views, but since they are called from the same controller action, javascript_intialization will generate the same javascript.
With options, you can provide a action_name in a view template and still use javascript_initalization at the application layout level to generate two different sets of javascript.
Possible modifications would be to allow it to take in a custom controller_class as well.
Let us know what you think!