Last active
December 19, 2015 13:29
-
-
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++).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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