Live data from Hacker News

Ask HN: JavaScript framework that I can use “right now”?

news.ycombinator.com

21–24 of 24 posts

Re: Ask HN: JavaScript framework that I can use “right now”?

#21
post #18
post #12

jQuery. I'm the sole developer on a "rails monolith" (254 tables, 540 models/classes, 4 years old), with enterprise-like architecture (ecommerce, event ticket sales, booking engine, access control, etc) and I'm positive the only way I'm able handle it all is because I stick to basic jquery. My secrets: - The rails-ujs adapter - $(document).on('event', '.element', ..) is fantastic. Never debug events ever again - sele…

Thank you for this comment. Several things there that I will look into! If you don't mind, could you elaborate a bit on the following: > - $(document).on('event', '.element', ..) is fantastic. Never debug events ever again > - a single javascript function that accepts a DOM element (body, a modal, section) and initializes all the elements the app knows of (selectize elements, date/time pickers, etc) inside that conta…

$(document).on lets you keep your javascript source files relatively flat because the event handler listens on the document, rather than an individual element. So even if the element is not currently on the page (loaded in via AJAX for example) the $(document).on handler will still pick it up. You never need to bind or unbind event handlers manually. The rails recommended way is to respond to the JS format and do server-rendered javascript.... but I tend to prefer using a simple data-transport-only (JSON) api, then do the client stuff on the client side.

Simple example:

  $(document).on 'ajax:success', '.guest-form', ->
    notify 'success', 'Guest updated.'
    $(@).parents('.modal').modal 'hide'


  $(document).ajaxError (event, jqxhr, settings, thrownError) ->
  handle_ajax_error(event, jqxhr)


  @handle_ajax_error = (error, xhr) ->
  if xhr.status is 422
    error = JSON.parse(xhr.responseText)['errors']
    notify 'error', error
  else if xhr.status is 500
    notify 'error', 'Error communicating with the server. We have been notified.'
  if xhr.status is 401
    notify 'warning', 'Session has expired. Redirecting to login...'
    window.location = '/login'
  if xhr.status is 403
    window.location = '/access-denied'

So if you load in content via AJAX (I do this a lot for table views of data and the user wants to see a single record), rather than go to a separate page, I do an ajax call, put the result into a Twitter bootstrap modal, show the modal, and when the bs.modal.shown event fires, that "single javascript function" with the modal as the parameter is called, and it initializes all the javascript needed for that particular DOM container.

Example:

  $(document).on 'shown.bs.modal', 'a[data-toggle="tab"]', (e) ->

    $modal_body = $( $(e.target).attr('href') )
    general_init $modal_body
The general_init function will initialize all my selectize elements (among other things) as such:

  general_init = ($container) ->
    add_selectize $container.find('input.membership-plan-select'), $.extend(true, {}, Selects.MembershipPlanOptions)
So when writing forms, lets say to create a new "Event Ticket" I simply plop in a text field with a class of "guest-select" and I instantly have an AJAX powered search field to find/create a guest to assign that ticket to.

Re: Ask HN: JavaScript framework that I can use “right now”?

#22
> I'm (literally) having nightmares about a JS bug I won't be able to solve > since it's so many moving parts

I am similar, writing the back end was easier ... front end not so much. I hated having JS bugs due to stupid typos and less than 100% code coverage in tests.

Switched to Typescript for the front end and things became much better. Doesn't have to be 'all or nothing' either, I still have some JS glue for interacting with some libraries and more dynamic bits, but generally everything is Typescript and feels much more solid.

FWIW the stack also heavily includes KnockoutJS, DataTables, Backbone's router & a smattering of JQuery plugins for this and that.

Re: Ask HN: JavaScript framework that I can use “right now”?

#23
post #9

I'd use Backbone. It's old (relative to other JS stuff) but stable. It's one thing to learn and has all the pieces (model, views, events, router). Plenty of resources online or in paper form. If you want, try applying some of the ideas of the "modern" tools to Backbone: make a backbone view that has two models - one for data (aka React props) and one for view state (aka React state). Bind both model "change" events t…

This is very, very pragmatic advice.

Re: Ask HN: JavaScript framework that I can use “right now”?

#24
post #21
post #18

Earlier quoted context omitted.

Thank you for this comment. Several things there that I will look into! If you don't mind, could you elaborate a bit on the following: > - $(document).on('event', '.element', ..) is fantastic. Never debug events ever again > - a single javascript function that accepts a DOM element (body, a modal, section) and initializes all the elements the app knows of (selectize elements, date/time pickers, etc) inside that conta…

$(document).on lets you keep your javascript source files relatively flat because the event handler listens on the document, rather than an individual element. So even if the element is not currently on the page (loaded in via AJAX for example) the $(document).on handler will still pick it up. You never need to bind or unbind event handlers manually. The rails recommended way is to respond to the JS format and do ser…

Thanks a lot for the detailed response! I'm a Rails developer myself, and have recently become increasingly interested in front end development. Have tried to embrace React et al. but am not convinced that it's what I need.
Post reply on HN