mauricemach / zappa

Node development for the lazy.
zappajs.org
MIT License
951 stars 85 forks source link

Alternate as options [z@appa 0.3 branch] #84

Open shimaore opened 13 years ago

shimaore commented 13 years ago

Diff: https://gist.github.com/1228579 contains two options:

zappa (_)->     # Context is already available as parameter.
  _.enable 'noclobber'

  # New: @ is data, parameter is context.
  _.get '/api/foo/:name': (_)->
    name = @name
    _.render 'foo'
zappa ->
  @enable 'dataparam'

  # New: parameter is data, @ is context.
  @get '/api/foo/:name': (_)->
    name = _.name
    @render 'foo'
mauricemach commented 13 years ago

Great idea! Very simple to implement and understand, gives a lot of flexibility and choice. I'll give it a try, and think about how it should interact with autoexport/autoimport.

Thanks!

shimaore commented 13 years ago

Just realized I didn't define "data" in the original gist, that's corrected.

mauricemach commented 12 years ago

OK, I implemented it as you described, the only difference being I'm using the setting value, as the two options are mutually exclusive.

So, in default 0.3.0 you receive and send data explicitly. Both the context and the param are references to the object with the zappa API appropriate for the situation:

zappa (foo) ->
  @get '/': ->
    funzo = @query.funzo
    funzo += '!'
    @render index: {funzo}

  # Alternative:
  foo.get '/': (bar) ->
    funzo = bar.query.funzo
    funzo += '!'
    bar.render index: {funzo}

  @on said: ->
    funzo = @data.funzo
    funzo += '!'
    @broadcast said: {funzo}

  @client '/index.js': ->
    @connect()

    @on welcome: ->
      funzo = @data.funzo

With the option below, the param becomes the merged collection of input variables, and it will also be sent to templates implicitly:

zappa ->
  @set databag: 'param'

  @get '/': (bar) ->
    bar.funzo += '!'
    @render 'index'

  @on said: (bar) ->
    bar.funzo += '!'
    @broadcast said: bar

  @client '/index.js': ->
    @connect()

    @on welcome: (bar) ->
      funzo = bar.funzo

And with this one, the same thing but with the context instead:

zappa (foo) ->
  foo.set databag: 'context'

  foo.get '/': (bar) ->
    @funzo += '!'
    bar.render 'index'

  foo.on said: (bar) ->
    @funzo += '!'
    bar.broadcast said: {@funzo}

  foo.client '/index.js': (fuu) ->
    fuu.connect()

    fuu.on welcome: ->
      funzo = @funzo
mauricemach commented 12 years ago

Note: databag: 'context' changed to databag: 'this'.