Created
September 6, 2012 03:08
-
-
Save elegantcoder/3650498 to your computer and use it in GitHub Desktop.
Template.coffee
This file contains hidden or 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
# BackboneJS 에 의존하는 템플릿 클래스. | |
# XML 로 로드해 템플리팅해서 리턴함 | |
# depends on backbone(Backbone.Event), jQuery(Ajax calling), underscore(Template engine) | |
root = root ? this | |
class root.Template | |
# 템플릿 XML 파일 로드 완료여부 | |
@ready = false | |
constructor: (@options = {templateURL: ''}) -> | |
@rawContents = '' | |
_.extend(@, Backbone.Events) | |
load: -> | |
that = @ | |
return false unless @options.templateURL | |
$.get(@options.templateURL, {}, (res) -> | |
that.rawContents = res | |
that.ready = true | |
that.trigger 'load' | |
return | |
, 'xml') | |
return | |
template: (templateName, data) -> | |
elements = @rawContents.getElementById(templateName) | |
return @_template.apply(@, [$.trim(node.nodeValue), data]) for node in elements.childNodes when node.nodeType is 4 | |
# 실제 템플릿 엔진을 적용하는 부분. Mustache 등 다른 엔진을 사용하고자 하는 경우 아 로직을 변경해 재구현하면 됨 | |
_template: (templateString, data) -> | |
_.template templateString, data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment