Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shimaore opened this issue Sep 20, 2011 · 4 comments
Open

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

shimaore opened this issue Sep 20, 2011 · 4 comments
Labels

Comments

@shimaore
Copy link
Contributor

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
Copy link
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
Copy link
Contributor Author

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

@mauricemach
Copy link
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
Copy link
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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants