My JavaScript templates / practices stink. Where can I go for help?

Over the past few years, I have been working almost exclusively on back-end tasks, and I just noticed that most JavaScript (and CoffeeScript) projects received my welcome in my absence.

I work mainly in a rail environment, and almost all of my JavaScript / jQuery is used to look like this:

$(an_element).an_event(function() { stuff_i_want_to_do; }) $(another_element).some_other_event(function() { some_other_stuff_i_want_to_do; }) 

Failures aside, it pretty much was.

In any case, I just looked at other people's code and noticed that in my absence many javascripters became much more beautiful. This is not complicated, but it is typical of the newer / better JavaScript approach I've seen:

 jQuery -> if $('#products').length new ProductsPager() class ProductsPager constructor: (@page = 1) -> $(window).scroll(@check) check: => if @nearBottom() @page++ $(window).unbind('scroll', @check) $.getJSON($('#products').data('json-url'), page: @page, @render) # nearBottom: => $(window).scrollTop() > $(document).height() - $(window).height() - 50 render: (products) => for product in products $('#products').append Mustache.to_html($('#product_template').html(), product) $(window).scroll(@check) if products.length > 0 

I was looking for resources for modern best practices / templates for JavaScript (and / or CoffeeScript), but I had no luck. So, in short, where should I look to be sure of success: the best javascript / coffeescript modern models and practices?

+47
javascript jquery ruby-on-rails coffeescript
Nov 17 '11 at 13:28
source share
7 answers

I like the Coffeebook Cookbook . This explains a lot and contains many examples.

You probably like the 12th chapter called Design patterns

+4
Nov 17 '11 at 13:32
source share

You need a good book, such as β€œ JavaScript Templates, ” followed by an equally good idea / environment like Fiddle , for practice.

+2
Nov 17 2018-11-11T00:
source share

I don't think reading general patterns will help you write really good code. Moderately good code, but not very good code. I would go to irc.freenode.net and ask in ## javascript and #coffeescript for help - at least there are a lot of people in #coffeescript to help you improve the code you added to gist .

0
Nov 17 2018-11-11T00:
source share

I do not see a problem with your old code. Or with a new code. Basically, just follow the same principles that you will follow with Ruby: mercilessly refactoring and let a good architecture emerge from refactoring.

0
Nov 17 2018-11-11T00:
source share

If you need to play with the full implementation of the large-scale JavaScript link architecture, see:

http://boilerplatejs.org

This is a set of templates and the integration of some good libraries with a ready-made sample application for starters. I wrote it to share my experience after working on several major JS projects.

0
Aug 20 2018-12-18T00:
source share



All Articles