       throw new TypeError typeof(callback) + ' is not a function'
     @_error = callback
 
-  # Sync client callback
-  sync: -> @_sync = true
-
   # User event script
-  script: (callback, argv) ->
+  remote: (callback, argv) ->
     if typeof callback isnt 'function'
       throw new TypeError typeof(callback) + ' is not a function'
     @_callback += callback.script argv
+
+  # parse data raw
+  parse: (raw) -> return JSON.parse raw
+
+  # User date model
+  model: (template, model) ->
+    # user template
+    if (typeof template is 'object') and (template isnt null)
+      model = template
+      template = 'base'
+    # data model
+    if (typeof model is 'object') and (model isnt null) and (!util.isArray model)
+      return if @query._key isnt 'script'
+      # user template
+      if typeof template is 'function'
+        @remote (-> func model), func: template, model: model
+      # template library
+      else
+        switch template
+          when 'base' #, ... more templates
+            @remote (-> app.core[key] model), key: template, model: model
+          # default
+          else @remote (-> app.core['base'] model), model: model
+      @end()
 
   # Respond client
   _do: (req, @_buffer) ->

      @_finish = null
      @_error? err
    # application's event callback
    @_sync = false
    @_callback = ''

  # End buffer
  end: ->
    if @_callback.length > 0
      @_buffer?.write "
        window.app._sync = #{@_sync.toString()};
        window.app._callback = function() {
          #{@_callback}
        };
      "
    @_buffer?.end.apply @_buffer, arguments

  # Write chunk to buffer
  write: -> @_buffer?.write.apply @_buffer, arguments

  # Write status and headers
  writeHead: -> @_buffer?.writeHead.apply @_buffer, arguments

  # Receive stream
  receive: (stream, encoding = 'utf8') ->
    return unless stream instanceof Readable
    stream.setEncoding = encoding if typeof encoding is 'string'
    stream.pipe @_buffer if @_buffer

# Application script
application = ->
  # application object
  app = window.app = (event, selector) ->
    window.event = event
    app._url = "#{location.pathname}?selector=#{selector}&_id=event&_key="
    return app._selector[selector]() if typeof app._selector[selector] is 'function'
    # getting event script
    $.getScript app._url + 'script'
    .done -> app._selector[selector]() if typeof app._selector[selector] is 'function'

  # application events
  app._selector = {}
  app.selector = (selector) -> app _event, selector

  # core data
  app.core = {
    # base data carrier and catcher
    base: (model) ->
      # get value from dom element
      get = (elem, name) ->
        switch name.toString()
          when 'text' then value = elem.text()
          when 'html' then value = elem.html()
          when 'value' then value = elem.val()
          else value = elem.attr name
        return value
      # user data
      data = {}
      for id, attrs of model
        elem = $('#' + id)
        data[id] = {}
        unless $.isArray attrs then data[id] = get elem, attrs
        else data[id][name] = get elem, name for name in attrs
      # set data
      carrier = (data) ->
        # set value to dom element
        set = (elem, name, value) ->
          switch name.toString()
            when 'text' then elem.text value
            when 'html' then elem.html value
            when 'value' then elem.val value
            else elem.attr name, value
        # user data
        for id, attrs of model
          elem = $('#' + id)
          continue unless data[id]
          unless $.isArray attrs then set elem, attrs, data[id]
          else set elem, name, data[id][name] for name in attrs
      # post data
      $.post app._url + 'data', (JSON.stringify data), carrier, 'json'
  }

  # getting user event
  window.constructor::__defineGetter__ '_event', ->
