Using custom helpers

74 views
Skip to first unread message

Eric Ponce

unread,
Feb 14, 2013, 5:27:12 AM2/14/13
to ged...@googlegroups.com
Hello!

I need to use an external function, for example:

function getColor(id){
    geddy.model.Color.first({id: id}, function(err, color){
         return color.name;
    });
}

And from some view I need to call the function, like this: (in EJS ) <%= getColor(1); %>

How can I do this?
thanks!

Tyler McGill

unread,
Feb 14, 2013, 10:35:30 AM2/14/13
to ged...@googlegroups.com
Hi Eric,

Here's what I am doing.  In app/controllers/application.js I have the following:

var Application = function () {
  
  // define
  this.h = {
    array: require('../helpers/array'),
    encrypt : require('../helpers/encrypt'),
    is: require('../helpers/is'),
    random: require('../helpers/random'),
    require: require('../helpers/require'),
    save: require('../helpers/save'),
    sort: require('../helpers/sort'),
    to: require('../helpers/to'),
    view: require('../helpers/view'),
    welcome: require('../helpers/welcome')
  }

and then in each controller I have this at the top:

var Notifications = function () {

  // responds with
  this.respondsWith = ['html', 'json', 'xml', 'js', 'txt'];

  // shortcuts
  var h = this.h;

That way I can access any function I need to in any controller by using h.helpername.fn

T


--
The official community discussion group.
website: geddyjs.org, source: https://github.com/mde/geddy, group: https://groups.google.com/d/forum/geddyjs?hl=en
---
You received this message because you are subscribed to the Google Groups "GeddyJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geddyjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Tyler McGill

Eric Ponce

unread,
Feb 14, 2013, 11:27:09 AM2/14/13
to ged...@googlegroups.com
Oh thanks!

This method is cool but....how can access to the helper file, from the view?

I have this now:

controllers/aplication:

this.h = {
        colours: require('../helpers/colours.js') //colour.js its a file, or folder? i've put colours.js
    }

In my colours controller:

// shortcuts
  var h = this.h;

In helpers/colours.js I have this:

funcion getColour(id){
    //code
    return "Hello";
}

And in the view:

<%= h.colours.getColour(1); %>

But the app crashes, what I am doing wrong?
Thanks friend!

Tyler McGill

unread,
Feb 14, 2013, 11:35:09 AM2/14/13
to ged...@googlegroups.com
just send it to your view in the respond function: self.respond({h: h, params: params});


--
The official community discussion group.
website: geddyjs.org, source: https://github.com/mde/geddy, group: https://groups.google.com/d/forum/geddyjs?hl=en
---
You received this message because you are subscribed to the Google Groups "GeddyJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geddyjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Tyler McGill

Eric Ponce

unread,
Feb 14, 2013, 11:57:08 AM2/14/13
to ged...@googlegroups.com
I've tried this but all the time i've recived the same error: Object #<Object> has no method 'getColour'
Have you any idea?

thanks for all!

jkinsman

unread,
Feb 14, 2013, 12:05:50 PM2/14/13
to ged...@googlegroups.com
You are missing a "t" when you are defining your function. You have funcion instead of function.

Try that and see if it fixes things.

Eric Ponce

unread,
Feb 14, 2013, 12:11:19 PM2/14/13
to ged...@googlegroups.com
Sorry!!! Its my mystake. In my app it's correctly writed but doen't work. thanks!

Eric Ponce

unread,
Feb 14, 2013, 8:46:18 PM2/14/13
to ged...@googlegroups.com
I've tried this:

In my application controller:

this.h = { a: require('../helpers/a.js') }
console.log( this.h.a );

And this responds { }

In "a.js" I have 2 functions, to try:

function some(){
    return "hello";
}

var hello = new(function(){ return "bye"; });

But I cant connect with any function :(

THANKS!!

der_On

unread,
Apr 26, 2013, 4:37:17 AM4/26/13
to ged...@googlegroups.com
Your file is missing the module.exports. See here for docs: http://nodejs.org/api/modules.html
Reply all
Reply to author
Forward
0 new messages