 #====== /Pagination ======#
 
 #====== Utility ======#
+Template.dataTable.getTemplateInstance = ->
+  return @templateInstance or false
+
+Template.dataTable.getGuid = ->
+  return @guid or false
+
+Template.dataTable.getData = ->
+  return @getTemplateInstance().data or false
+
+Template.dataTable.setData = ( key, data ) ->
+  @templateInstance.data[ key ] = data
+
 Template.dataTable.setDefaultCellValue = ( column ) ->
   Match.test column.mData, String
   Match.test column.sTitle, String

  instantiatedComponent.initialize()

Template.dataTable.destroyed = ->


Template.dataTable.initialize = ->
  @prepareQuery()

Template.dataTable.getTableState = ->
  return @getData().tableState or false

Template.dataTable.fnServerData = ( sSource, aoData, fnCallback, oSettings ) ->
  # prepare table state ( sort, filter )
  @setTableState aoData
  # prepare subscription options ( pagination, limit, sort )
  @setSubscriptionOptions
    skip: @getTableState().iDisplayStart
    limit: @getTableState().iDisplayLength
    sort: @getTableState().sort
  # subscibe to the dataset matching the current table state
  @setSubscriptionHandle Meteor.subscribe( @getSubscription(), @getTableState().query, @getSubscriptionOptions() )
  # run the datatables server callback when the subscription is ready
  @setSubscriptionAutorun Deps.autorun =>
    if @getSubscriptionHandle() and @getSubscriptionHandle().ready()
      @log 'fnServerdData:handle:ready', @getSubscriptionHandle().ready()
      # setup local cursor options ( identical to server except no skip )
      cursorOptions =
        skip: 0
        limit: @getTableState().iDisplayLength
        sort: @getTableState().sort
      # set local cursor to monitor the current table state
      @setCursor @getCollection().find @getTableState().query, cursorOptions
      # fetch the data for the current table state
      aaData = @getCursor().fetch()
      @log 'fnServerData:aaData', aaData
      # call the datatable server callback with the current table state ( called from the client )
      fnCallback
        # An unaltered copy of sEcho sent from the client side.
        # This parameter will change with each draw (it is basically a draw count)
