Docker Test

by Paweł Niemczyk

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.wreqr/0.1.0/backbone.wreqr.min.js"></script>
Test

CoffeeScript

class TransactionDocker
  class Container
    type: 'container'
    constructor: (@id, @request, @response) ->
      @name = request.name
    success: (fn) -> @response.success(fn)
    error: (fn) -> @response.error(fn)
    always: (fn) -> @response.always(fn)
              
  class Backet
    squence: []
    list:    {}
    constructor: (@name, @sender) ->
    push: (id, opts) -> 
      @list[id] = opts
      if opts.priority then @squence.unshift(id) else @squence.push(id)
      response = null
      response = $.ajax(opts).always( => @provide(id, response))
    
    provide: (id, response) ->
      # containering
      @list[id] = new Container(id, @list[id], response)
      @send()
    send: ->
      for i in @squence
        return unless @list[i].type is 'container'
        @squence.pop
        @sender(@list[i])
        delete @list[i]
             
          #send all done containers from top of the list
  constructor: (opts={}) -> @commands = opts.commands
  fifo: {}
  execute: (id, opts={}) ->
    priority = opts.priority is true
    ns       = opts.ns || 'default'
    @push(id, ns, priority, opts)
  
  push: (id, ns, priority, opts) ->
    return @container(id, ns, priority, opts) if ns is 'default'
    @fifo[ns] = new Backet(ns, @sender) unless @fifo[ns]
    @fifo[ns].push(id, opts)
  
  container: (id, ns, priority, opts) ->   
    console.log id
    response = null
    response = $.ajax(opts).always( => @sender(new Container(id, opts, response)))        
      
  sender: (container) => @commands.execute("tcontainer-#{container.name}", container)

log = (e) -> console.log(e)
commands = new Backbone.Wreqr.Commands()
commands.addHandler('tcontainer-geo', (c) -> 
  log(c)
  log(c.success((data, status) -> log data; log status))
)
t = new TransactionDocker(commands: commands)
t.execute(1, url:'/echo/html/', ns: 'test', name: 'geo')