Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#
# core
from 'log' import log
from 'jquery' import $
from 'underscore' import _
from 'backbone' import Backbone
from 'handlebars' import Handlebars
# iterator: use to truthy-test filter function to iterate over a backbone collection, only rendered those
# which pass the truthy-test
Handlebars.registerHelper "filter", (coll, filter, options) ->
# coll: collection of backbone models to loop over
# filter: function to filter array, should return a bool wether to render an item in coll
# fn: handlebars built-in, a function to render the contents of the block given a context
# elseFn: like fn, but used with the {{else}} block
buffer = ''
if coll?.length > 0
coll.each (each) =>
if filter(each)
@model = {'model': each}
buffer += options.fn(@)
return buffer
else
return options.inverse(@)
# accessor: get a specific property out of a backbone model
Handlebars.registerHelper "get", (model, key) ->
# model: model to access a property of
# key: key on the model to display the value of here
return model.get(key)
# boolean: a better version of the built-in `if` helper which accounts for SafeStrings
Handlebars.registerHelper "if", (value, options) ->
# value: value to test for truthyness
if value instanceof Handlebars.SafeString
value = value.string
if value? and value
return options.fn(@)
else
return options.inverse(@)
# boolean: a better version of the built-in `unless` helper which accounts for SafeStrings
Handlebars.registerHelper "unless", (value, options) ->
# value: value to test for truthyness
if value instanceof Handlebars.SafeString
value = value.string
if value? and value
return options.inverse(@)
else
return options.fn(@)
# boolean: render if the passed backbone collection has as least one item in it
Handlebars.registerHelper "atLeastOne", (collection, options) ->
# value: value to test for truthyness
if collection instanceof Backbone.Collection and collection.length > 0
return options.fn(@)
else
return options.inverse(@)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.