Created
July 24, 2015 13:53
-
-
Save code-for-coffee/e8d4be5ace2877cab2ce to your computer and use it in GitHub Desktop.
Intro to Underscore.js
This file contains 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
var woot = woot || {}; | |
woot.log = function(key) { | |
console.log(key); | |
} | |
// mimics Collection.each | |
// http://underscorejs.org/#each | |
// args: | |
// _.each(list, iteratee, [context]) | |
_.each(['Aldrin', 'Armstrong', 'Grissom', 'Hadfield'], woot.log); | |
// make a super string using Template (same syntax as ERB) | |
// 1. Create a data object... | |
// ...or use one from an Ajax call? | |
var data = { | |
name: 'Tom', | |
hobby: 'learning JS', | |
beer: '3 Floyds' | |
}; | |
// 2. Build a template | |
// call it superString | |
// check out that <%= variable_name %> | |
var superString = _.template("<%= name %> really enjoys a <%= beer %> while <%= hobby %>"); | |
console.log(superString); | |
// 3. Super string is now a template function. It accepts one argument: a data object with keys that correspond to the <%= variable_name %> | |
var compiledTemplate = superString(data); | |
// 4. Log the template | |
console.log(compiledTemplate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment