Skip to content

Instantly share code, notes, and snippets.

@Dykam
Created September 4, 2012 14:59
Show Gist options
  • Select an option

  • Save Dykam/3621979 to your computer and use it in GitHub Desktop.

Select an option

Save Dykam/3621979 to your computer and use it in GitHub Desktop.
$.fn.replaceTag = (replacement) ->
result = new jQuery.fn.init
for element in this
$element = $(element)
attributes = {}
for attribute in element.attributes
attributes[attribute.name] = attribute.value
changed = $element.wrapInner($("<#{replacement}>", attributes)).children().unwrap()
result = result.add changed
result
$.fn.copyAndFillTemplate = (fields) ->
result = new jQuery.fn.init
for element in this
clone = @.clone()
becomes = clone.data("becomes") ? "div"
clone.removeData("becomes")
clone = clone.replaceTag(becomes)
for field in $("field", clone)
$field = $(field)
name = $field.text()
unless fields[name]
$field.remove()
next
becomes = $field.data("becomes") ? "span"
$field.removeData("becomes")
$field.replaceTag(becomes).text(fields[name])
result = result.add clone
result
<div class="InfoWindow">
<div><span>description</span></div>
<fieldset class="Directions">
<legend>Route</legend>
<div class="Container">
<label>Van</label><input type="text" class="Dest" />
<label>naar</label><span class="Wrapped">address</span><input type="submit" value="Route zoeken" class="Search" />
</div>
</fieldset>
</div>
<template class="InfoWindow" data-becomes="div">
<div><field>description</field></div>
<fieldset class="Directions">
<legend>Route</legend>
<div class="Container">
<label>Van</label><input type="text" class="Dest" />
<label>naar</label><field class="Wrapped">address</field><input type="submit" value="Route zoeken" class="Search" />
</div>
</fieldset>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment