Skip to content

Instantly share code, notes, and snippets.

@variousauthors
Last active December 19, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save variousauthors/5962335 to your computer and use it in GitHub Desktop.
Save variousauthors/5962335 to your computer and use it in GitHub Desktop.
My very first closure in javascript! I talked about being able to do this in the interview, but my understanding was mainly theoretical (although this is very similar to specializing functors in C++).
$(function() {
var annex = $('.page-insert-annex');
var handle = annex.data()['handle'];
/* get a closure around annex and handle */
var closure = populate_annex_with_page(annex, handle);
if (annex.length) {
$.when($.get('/pages.json')).done(closure); // be sneaky
}
});
function populate_annex_with_page(annex, handle) {
return function(result) {
var pages = result.pages;
var page = $.grep(pages, function(page) {
return page.handle == handle;
})[0];
annex.html(page.body_html);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment