Basic Question

20 views
Skip to first unread message

Paul Tiseo

unread,
Oct 5, 2015, 11:08:23 AM10/5/15
to actionHero.js
Say I have a complex task that has multiple steps and want to break it up into separate functions. What is the best way to do this while keeping all the code in the same JS file for that task? Do I define functions inside run(), do I define them at the same scope/level as run() (but then how do I call them inside run()?), etc. What do AH devs usually do?

Evan Tahler

unread,
Oct 5, 2015, 1:29:29 PM10/5/15
to actionHero.js
You don't actually need to keep all of your steps in one file!  You can certainly require things into a task file.

Personally, I'm of the opinion that most of my complex logic is defined in an initializer or a model file, which then exposes methods to the api object.  I can also then share the same logic between both tasks and actions (here's a more complex example site using model-methods within an action).  

For example, say I had an abandoned cart email I wanted to send users of an e-commerce site.  Every hour I would have a periodic task run that checks all carts older than a day, checks that the products are in stock, and then sends an email.  If all of my logic was in initializers and models, my task would look something like: https://gist.github.com/evantahler/192337ba5ea521e00c87

Michael Jensen

unread,
Oct 5, 2015, 2:11:46 PM10/5/15
to Evan Tahler, actionHero.js
To echo Evan's response, I usually try to keep my tasks as small as possible and put almost all the logic into an initializer. This makes your initializer more testable and makes it easier to eliminate flakiness in your task code.

We tried it the other way - with a lot of logic in tasks - and ended up with a 500 line file which was pretty hard to navigate.

--
You received this message because you are subscribed to the Google Groups "actionHero.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to actionhero-j...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Tiseo

unread,
Oct 5, 2015, 2:41:04 PM10/5/15
to actionHero.js, evant...@gmail.com
So, if I understand this correctly, one possible good practice is to use initializer files to "package" your functions, and load said functions on to the api object. So, for example, if I have a bunch of parsing functions, I would create a parsing.js initializer file that "mounts" my functions on api.parsers or some such namespace-like object hanging off the global api object?

Evan Tahler

unread,
Oct 5, 2015, 2:48:51 PM10/5/15
to actionHero.js, evant...@gmail.com
exactly.

Check out the tutorial.  Here's a 'blog' initilizer where all the methods for adding and working with blog posts are kept: https://github.com/evantahler/actionhero-tutorial/blob/master/initializers/blog.js and then actions are just wrappers around them: https://github.com/evantahler/actionhero-tutorial/blob/master/actions/blog.js.  Tasks would be the same 
Reply all
Reply to author
Forward
0 new messages