handlebars & namespaces
class overview
- Homework solution
- Handlebars
- Namespaces
- JavaScript constructors
- The 'this' keyword
- jQuery event handlers
- Controlling time in JS
- Homework overview
HANDLEBARS
- Roughly follows the Mustache specification
- Provides a super-easy way to create dynamic html with JavaScript objects.
- Store your templates in your html.
- Include Handlebars by downloading, Bower, or cdn.
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.2/handlebars.min.js"></script>
- How to render a template with an object
var rawTemplate = $('#itemTemplate').html();
var compiledTemplate = Handlebars.compile(rawTemplate);
var finishedHtml = compiledTemplate(obj);
$('#items').append(finishedHtml);
HANDLEBARS TEMPLATES
Template:
<script id="itemTemplate" type="text/x-handlebars-template">
<div class="header">{{title}}</div>
<div class="body">
<div>{{type}}</div>
<div>{{price}}</div>
</div>
</script>
Object:
var product = {
title : "Cheese",
type : "Sharp Cheddar",
price : "$3.49"
}
HANDLEBARS BLOCKS
- Blocks create looping or conditional behavior
- Typically used to determine whether properties exist or for accessing sub-arrays.
- If block:
{{#if type}}
<div>Type: {{type}}</div>
{{else}}
<div>Type: None</div>
{{/if}}
- Each block:
{{#each products}}
<div>{{title}}</div>
<div>{{price}}</div>
{{/each}}
HANDLEBARS HELPERS
- Enables you to create your own custom tags with behavior.
- Useful for formatting & custom html behavior
- Think about redesigning your objects before creating a new helper.
- Register a helper in JS. First arg is name, second is a function to determine output. Can take arguments.
Handlebars.registerHelper('formatMoney', function(arg1) {
return Handlebars.SafeString('$' + arg1);
});
- Using it in your Handlebars template:
{{formatMoney price}}
//price is a property from your object
namespaces
- Very useful for organizing code & abstracting ideas.
- Namespaces use Object literal declaration.
- Can't make new objects from it.
- Shouldn't hold state.
- Good for utility functions, static properties.
- Why use Namespaces?
- Keeps Global scope clean.
- Adds context to your functions.
- Allows you to cleanly share any code.
TYPICAL CODE ORGANIZATION
- Declarations first.
- Objects/Variables
- Functions
- Code to actually run comes last.
Object Constructors
- jsbin examples
- Allow you to create reproducible Objects
- Can use instanceof to check Object type
- Perfect for Objects that hold state
- Enables use of the 'this' keyword inside a function
- Used when you are creating a multiple objects that will have the same structure but different contents.
//constructor declaration
function CustomObject() {};
//using constructor to create an object
var myObj = new CustomObject();
myObj instanceof CustomObject;
// > true
this
- Reserved keyword
- The 'this' variable always refers to the current object.
- Works in functions that are constructors, but not normal functions.
cool js thing for the day
Local applications built from Chrome using HTML, CSS, & JavaScript
AJS Lecture 6: Handlebars & Namespaces
By Ryan Lewis
AJS Lecture 6: Handlebars & Namespaces
- 661