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

If you add datetime picker as nested attribute, it won't work #1651

Closed
mspanc opened this issue Sep 9, 2012 · 10 comments
Closed

If you add datetime picker as nested attribute, it won't work #1651

mspanc opened this issue Sep 9, 2012 · 10 comments

Comments

@mspanc
Copy link
Contributor

mspanc commented Sep 9, 2012

I found bug in activeadmin 0.5.0.

If I define nested set in form, using .has_many declaration, newly added fields that should be datepickers, are not active. Propably JS hooks for datepickers are called only on page load, not after adding each child record.

I've added following code to active_admin.js and it solves the issue:

$('a.button').live('click', function() {
  $('input.datepicker:not(.hasDatepicker)').datepicker();
});

Maybe it should be added to default activeadmin JS?

@jpmckinney
Copy link
Contributor

Duplicate #1646?

@mspanc
Copy link
Contributor Author

mspanc commented Sep 12, 2012

Nope, it is not a duplicate, it is separate issue.

#1646 is about the fact, that active admin css prevents using date/time addon to standard jquery ui date picker (but standard date picker works fine).

This issue is about the fact, that even if you use standard and working date picker (without time addon) and put it in nested set, dynamically added fields with date are not reacting to click.

@jpmckinney jpmckinney reopened this Sep 12, 2012
@mspanc
Copy link
Contributor Author

mspanc commented Jan 21, 2013

live() syntax is obsolete now in jQuery, proper code is

$(document).on('click', 'body.active_admin a.button', function() {
  $('input.datepicker:not(.hasDatepicker)').datepicker();
});

@seanlinsley
Copy link
Contributor

@saepia, isn't this a better option? (replacing the version in the AA initializer)

$(".datepicker").on 'focus', ->
  $(this).datepicker dateFormat: 'yy-mm-dd'

@mspanc
Copy link
Contributor Author

mspanc commented Jan 21, 2013

@daxter your code will not handle date pickers added in nested forms, after the AA initializer is called and will unnecessarily call datepicker() on fields that are already datepickered.

Something between my and your proposal could be

$(document).on 'focus', 'body.active_admin input.datepicker:not(.hasDatepicker)', ->
  $(this).datepicker dateFormat: 'yy-mm-dd'

It should work on all fields, regardless if they were rendered by rails or added later in JS and has as narrow selector as it is reasonable.

@seanlinsley
Copy link
Contributor

Shouldn't you write it the opposite way? Is there some benefit to referencing document that I don't know about?

$('.active_admin .datepicker:not(.hasDatepicker)').on 'focus', ->
  $(this).datepicker dateFormat: 'yy-mm-dd'

I don't think it's necessary to define body and input in the selector -- it's both slower to execute and harder to read.

@mspanc
Copy link
Contributor Author

mspanc commented Jan 21, 2013

The code you wrote is not going to work for nested forms as it binds event
handler only to elements existing during this call.

Please refer to live() docs in jQuery manual, it is clearly stated that
what I wrote is correct equivalent in new jQuery.

BTW AA's jquery gem dependency should match version that introduced this
syntax

I don't think performance and readability is key issue here as AA UI is
quite light and no so much people reads its code because they just put a
gem into their gemfile and forget.

@seanlinsley
Copy link
Contributor

I disagree that the distinction is clearly stated in the docs, but a quick jsfiddle confirmed your point.

@mspanc
Copy link
Contributor Author

mspanc commented Jan 22, 2013

From: http://api.jquery.com/live

Rewriting the .live() method in terms of its successors is straightforward; these are templates for equivalent calls for all three event attachment methods:

$(selector).live(events, data, handler); // jQuery 1.3+
$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+
$(document).on(events, selector, data, handler); // jQuery 1.7+

@macfanatic
Copy link
Contributor

#1879 has been merged, should resolve this issue.

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

No branches or pull requests

4 participants