Skip to content

Commit

Permalink
Facile now works as a Node.js module.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjpowers committed Jul 10, 2012
1 parent 463c7da commit fba9d51
Show file tree
Hide file tree
Showing 9 changed files with 696 additions and 512 deletions.
27 changes: 20 additions & 7 deletions facile.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$ = this.$ || require "cheerio"

find = ($el, key) ->
$result = $el.find('#' + key)
$result = $el.find('.' + key) if $result.length == 0
Expand All @@ -12,7 +14,7 @@ combineClasses = (existingClasses, newClasses) ->
else
newClasses

window.facile = (template, data) ->
facile = (template, data) ->
$template = $('<div />').append($(template))
for key, value of data
bindOrRemove($template, key, value)
Expand Down Expand Up @@ -42,7 +44,7 @@ bindArray = ($template, key, value) ->
if $nested.length > 0
$root = $nested

if $root.is('table')
if tagName($root) == "TABLE"
$root = $root.find('tbody')

$child = $root.children().remove()
Expand All @@ -61,22 +63,28 @@ bindObject = ($template, key, value) ->
else
bindNestedObject($template, key, value)

tagName = ($el) ->
if $el.prop
$el.prop "tagName"
else
$el[0].name.toUpperCase()

bindValue = ($template, key, value) ->
if key.indexOf('@') != -1
[key, attr] = key.split('@')
$el = find($template, key)
if $el.prop('tagName') == 'SELECT'
if tagName($el) == 'SELECT'
$el.find("option[value='#{value}']").attr('selected', 'selected')
else
$el.attr(attr, value)
else
$el = find($template, key)
if $el.length > 0
if $el.prop('tagName') == 'INPUT' && $el.attr('type') == 'checkbox' && value
$el.attr('checked', true)
else if $el.prop('tagName') == 'INPUT' || $el.prop('tagName') == 'OPTION'
if tagName($el) == 'INPUT' && $el.attr('type') == 'checkbox' && value
$el.attr('checked', "checked")
else if tagName($el) == 'INPUT' || tagName($el) == 'OPTION'
$el.attr('value', '' + value)
else if $el.prop('tagName') == 'SELECT' && value.constructor != Object
else if tagName($el) == 'SELECT' && value.constructor != Object
$el.find("option[value='#{value}']").attr('selected', 'selected')
else
$el.html('' + value)
Expand All @@ -92,3 +100,8 @@ bindAttributeObject = ($template, key, value) ->
$template.attr('class', combineClasses($template.attr('class'), attrValue))
else
$template.attr(attr, attrValue)

if this.window
window.facile = facile
else
module.exports = facile
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"author": "Sean Massa",
"name": "facile",
"description": "Convention-based template engine that depends on jQuery, zepto or cheerio.",
"version": "0.1.0",
"contributors": [
"Chris Powers <chrisjpowers@gmail.com> (http://chrisjpowers.com)"
],
"repository": {
"type": "git",
"url": "git://github.com/endangeredmassa/facile.js.git"
},
"main": "index.js",
"scripts": {
"test": "jasmine-node --coffee spec"
},
"dependencies": {
"cheerio": "*",
"coffee-script": "*"
},
"devDependencies": {
"jasmine-node": "*"
},
"engines": {
"node": "*"
}
}
187 changes: 0 additions & 187 deletions spec.coffee

This file was deleted.

Loading

0 comments on commit fba9d51

Please sign in to comment.