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

plugin API #7

Open
ghost opened this issue Nov 21, 2010 · 6 comments
Open

plugin API #7

ghost opened this issue Nov 21, 2010 · 6 comments

Comments

@ghost
Copy link

ghost commented Nov 21, 2010

Hi!
Is it possible to define global function like view in included coffeescript file?
example:
mongoose = require('mongoose').Mongoose
db = mongoose.connect 'mongodb://localhost/db'

model = (pair) ->
  for name, body of pair
    mongoose.model name, body
    pair[name] = db.model name
    def pair
@mauricemach
Copy link
Owner

Hi Rafał,

Short answer: as a quick dirty hack you could accomplish that by declaring global.model = ..., but you'd be polluting the global namespace, affecting every lib that gets loaded into the runtime. Which leads me to the longer answer...

Zappa actually doesn't use globals anywhere. Your app's code is just dynamically compiled into a function where things like get, view, etc are made available as local variables before your code.

Currently there isn't a way to add local variables to the app scope, the way we do to the request/message handler scope with def, helpers etc. It would be indeed a useful feature though, so I'm flagging it and will add it in a future version.

@525c1e21-bd67-4735-ac99-b4b0e5262290

+1 for a global "registry"

ATM we use the def hack...

server.coffee
1. registry = {}
2. def registry: registry

@mauricemach
Copy link
Owner

Not sure what you mean by a "global registry". What is it that you're trying to solve with that?

@rdrey
Copy link

rdrey commented Aug 19, 2011

sorry to bump an old issue, but I'm also struggling with where to require my db-handler and how to make it available consistently across views. if you could write a little example on how you'd use cradle / mongoose with zappa, that would be sweet!

I'm currently doing a requiring 'cradle' at the zappa scope, but trying to use it in a self-defined setup method fails.

requiring 'util', 'cradle'

def setup: () ->
  app.db = new(cradle.Connection)().database('rainer')
  app.db.create()
  app.db.save
    leila: "isAwesome"
    rainer: "isLazy"
    , (err, res) ->
      if err
        console.log err
      else
        console.log "Success"  

get '/': ->
  setup() 

ReferenceError: cradle is not defined

@mauricemach
Copy link
Owner

defs are not modified in any way, so they're not affected by zappa's requiring - actually, they don't need it, because they have access to the normal scope. helpers are the opposite.

So to use def:

cradle = require 'cradle'

def setup: ->
  app.db = new(cradle.Connection)().database('rainer')

If you had defined that as a helper you'd need requiring:

# Equivalent to def cradle: require 'cradle'
requiring 'cradle'

helper setup: ->
  app.db = new(cradle.Connection)().database('rainer')

But if you don't need access to any HTTP handling stuff in setup, you should keep it as a def.

@rdrey
Copy link

rdrey commented Aug 20, 2011

Aah, that explains it. Thanks! I will finish reading Javascript: The Good
Parts and then zappa's source ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants