Permalink
Browse files

Fix setting of mimetypes

  • Loading branch information...
fhemberger committed Sep 11, 2011
1 parent 1de091e commit 395ff7663a8706988d236dd4a86a011894c30554
Showing with 21 additions and 9 deletions.
  1. +21 −9 lib/view.coffee
View
@@ -3,15 +3,18 @@ fs = require 'fs'
class View
- @_mimeType = 'text/plain'
constructor: (filename, options = {}) ->
+ @_mimeType = 'text/plain'
+ @loadTemplate filename
+
+ loadTemplate: (filename, options = {}) ->
templateFile = fs.readFileSync(filename, 'utf8')
@_template = jade.compile(templateFile, options)
render: (context = undefined, locals = {}) ->
body = @_template.call(@, locals)
-
+
if context
context.writeHead 200,
'Content-Type': "#{@_mimeType}; charset=utf-8"
@@ -21,29 +24,38 @@ class View
context.end body
else
body
-
+
class HTMLView extends View
- @_mimeType = 'text/html'
+
+ constructor: (filename, options = {}) ->
+ @_mimeType = 'text/html'
+ @loadTemplate filename
class JSONView extends View
- @_mimeType = 'application/json'
- constructor: () ->
-
+ constructor: (filename, options = {}) ->
+ @_mimeType = 'application/json'
+
render: (context = undefined, locals = {}) ->
@_template = (locals) ->
JSON.stringify locals
super context, locals
class RSSView extends View
- @_mimeType = 'application/rss+xml'
+
+ constructor: (filename, options = {}) ->
+ @_mimeType = 'application/rss+xml'
+ @loadTemplate filename
class ATOMView extends View
- @_mimeType = 'application/atom+xml'
+
+ constructor: (filename, options = {}) ->
+ @_mimeType = 'application/atom+xml'
+ @loadTemplate filename
class ViewFactory

0 comments on commit 395ff76

Please sign in to comment.