Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Open
shimaore opened this Issue · 4 comments

2 participants

@shimaore

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

  • Existing data style: Setting 'noclobber' allows @ to contain only data while the zappa context is passed as an argument. Keeps existing (0.1/0.2) @ semantics.
zappa (_)->     # Context is already available as parameter.
  _.enable 'noclobber'

  # New: @ is data, parameter is context.
  _.get '/api/foo/:name': (_)->
    name = @name
    _.render 'foo'
  • Shortcut data style: Setting 'dataparam' allows @ to contain the context while the parameter contains the data. Allows for shorter access to data.
zappa ->
  @enable 'dataparam'

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

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

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

@mauricemach
Owner

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
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.