   instantiatedComponent.initialize()
 
 Template.dataTable.destroyed = ->
-
+  @log "destroyed"
 
 Template.dataTable.initialize = ->
   @prepareQuery()

Template.dataTable.getOptions = ->
  return @getData().options or @presetOptions() or false

# Prepares the options object by merging the options passed in with the defaults
Template.dataTable.prepareOptions = ->
  options = @getOptions() or {}
  options.aaData = @getRows() or []
  options.aoColumns = @getColumns() or []
  if @getCollection() and @getQuery()
    options.bServerSide = true
    options.sAjaxSource = "useful?"
    options.fnServerData = @fnServerData.bind @
  @setOptions _.defaults( options, @defaultOptions )

Template.dataTable.mapTableState = ( aoData ) ->
  aoData = @arrayToDictionary aoData, 'name'
  @log 'mapTableState:aoData', aoData
  tableState =
    sEcho: aoData.sEcho.value or 1
    bRegex: aoData.bRegex.value or false
    iColumns: aoData.iColumns.value or 0
    iDisplayLength: aoData.iDisplayLength.value or 10
    iDisplayStart: aoData.iDisplayStart.value or 0
    iSortingCols: aoData.iSortingCols.value or 0
    sColumns: aoData.sColumns.value or ""
    sSearch: aoData.sSearch.value or ""
    columns: []
  getDataProp = ( key, index ) ->
    key = "#{ key }_#{ index }"
    return aoData[ key ].value
  mapColumns = ( index ) ->
    tableState.columns[ getDataProp 'mDataProp', index ] =
      mDataProp: getDataProp 'mDataProp', index
      bRegex: getDataProp 'bRegex', index
      bSearchable: getDataProp 'bSearchable', index
      bSortable: getDataProp 'bSortable', index
      sSearch: getDataProp 'sSearch', index
  mapColumns index for index in [ 0..( tableState.iColumns - 1 ) ]

  if tableState.sSearch isnt ""
    searchQuery = $or: []
    mapQuery = ( key, property ) ->
      unless key is '_id'
        if tableState.sSearch isnt ""
          obj = {}
          obj[ key ] =
            $regex: tableState.sSearch
            $options: 'i'
          searchQuery.$or.push obj

    for key, property of tableState.columns
      mapQuery key, property

    if @getQuery is {}
      tableState.query = searchQuery
    else
      tableState.query =
        $and: [
          @getQuery()
          searchQuery
        ]
  else tableState.query = @getQuery()

  if tableState.iSortingCols > 0
    tableState.sort = {}
    mapSortOrder = ( sortIndex ) ->
      sortIndex = sortIndex - 1
      propertyIndex = getDataProp 'iSortCol', sortIndex
      propertyName = getDataProp 'mDataProp', propertyIndex
      switch getDataProp( 'sSortDir', sortIndex )
        when 'asc' then tableState.sort[ propertyName ] = 1
        when 'desc' then tableState.sort[ propertyName ] = -1
    mapSortOrder sortIndex for sortIndex in [ 1..tableState.iSortingCols ]

  return tableState

Template.dataTable.setTableState = ( aoData ) ->

    sTitle: "id"
    mData: "_id"
    bVisible: false
    bSearchable: false
  # a function to add a default mRender function if one is not defined
  # iterate over the columns array and add mRender to the columns that need it
  @setDefaultCellValue column for column in columns
