Showing posts with label partial. Show all posts
Showing posts with label partial. Show all posts

Thursday, December 11, 2014

Lo-Dash: Partials and References

The partial() function in Lo-Dash is used to compose higher-order functions, with argument values partially applied. So for example, let's say we have a function that accepts a string argument, like console.log(). We can compose a new function that when called, doesn't need to supply a string argument — it'll use the argument value it was composed with. This is especially powerful in contexts where we don't necessarily have control over how the function is called. As is the case with callback functions. Primitive types, like strings and numbers work well as partial arguments. Things like arrays and objects work fine too, if they're passed to partial() as a literal. If a reference is passed as a partial argument, unexpected things start to happen.

Friday, April 25, 2014

Lodash: Replacing setTimeout() With delay()

The built-in setTimeout() JavaScript function defers the the execution of a given function till a given number of milliseconds have passed. For the most part, this works perfectly — you pass in a function, and the duration to wait. Things aren't so straightforward when you already have a function defined, and you want to delay it's execution — especially when you want to invoke the function with specific parameters.

Wednesday, March 5, 2014

Recursive List Building With Handlebars

I always end up implementing some convoluted JavaScript inside a Handlebars helper whenever I have to render lists with any depth. Think trees — there could be several nested lists. I don't like the JavaScript approach I've taken in the past, I'm using Handlebars to generate dynamic markup — it should be able to handle nested lists, right? My preferred approach these days is to use a partial template, and to call that recursively.

Wednesday, February 5, 2014

Handlebars: Overriding Global Partials

The HandlebarsJS API let's you register partial templates — templates that get rendered within the context of a larger template — in a global scope. These partials are available to all other templates. For example, if there's a section of markup repeated throughout many of your templates, rather than repeat yourself, you could compose a partial template. You would then make this available to all other templates by using the registerPartial() function. This helps simplify larger templates too, by breaking them down into smaller, more manageable parts. However, if you're using the same global partial in all your templates, you're probably going to run into a situation where you need to swap the default partial for something more specific. Let's say one of your pages requires a custom header partial. We can pass in a custom partial that's used in that context only.