static Method Use

59 views
Skip to first unread message

Eric Ponce

unread,
Nov 8, 2012, 6:29:18 AM11/8/12
to ged...@googlegroups.com
Hi!

I have two models: Articles and Events
In the index function of Articles Controller, i've tried to do this:

geddy.model.Article.all({},{sort: {createdAt:"desc"}},function(err, articles) {
    self.respond({params: params, articles: articles, events: geddy.model.Event.getFutureEvents()});
});

and in te Event Model

Event.getFutureEvents = function () {
var today = new Date();
Event.all({when:{gt: today}},function(err,events){
            //"when" is the Event date
            return events;
        });
}

But in the controller I don't recibe anything.
If I put console.log(events) in the Event Model I can see all events.

Can you help me?
Thanks! :D

Matthew Eernisse

unread,
Nov 8, 2012, 1:40:24 PM11/8/12
to ged...@googlegroups.com
You can't just `return` from an asynchronous function. You'll have to
use a callback, like this:

Event.getFutureEvents = (callback) {
var today = new Date();
Event.all({when:{gt: today}},function(err,events){
//"when" is the Event date
if (err) { callback(err, null); }
else {
callback(null, events);
}
});
};
> --
> The official community discussion group.
> website: geddyjs.org, source: https://github.com/mde/geddy, group:
> https://groups.google.com/d/forum/geddyjs?hl=en
>
>

Eric Ponce

unread,
Nov 8, 2012, 2:18:06 PM11/8/12
to ged...@googlegroups.com
thanks Matthew but your code give me an error:

Event.getFutureEvents = (callback) { 
                                   ^
SyntaxError: Unexpected token {

:O i've tried to solve it but i can't...

Matthew Eernisse

unread,
Nov 8, 2012, 2:22:34 PM11/8/12
to ged...@googlegroups.com
Ugh, stupid. Should have done it in a real text-editor. Should be:

Event.getFutureEvents = function (callback) {

You'll need to pass it a callback function that does your `respond`.

Eric Ponce

unread,
Nov 8, 2012, 8:35:34 PM11/8/12
to ged...@googlegroups.com
OK! :D it's working. I recive the events array. But i can't use. I have this:

In Articles Controller:
this.index = function (req, resp, params) {
    var self = this;
    geddy.model.Event.getFutureEvents(function(nothing,events){
        console.log(events);
    });

    geddy.model.Article.all({},{sort: {createdAt:"desc"}},function(err, articles) {
        self.respond({params: params, articles: articles, events: events});
    });
};

Console log prints all events BUT, how can I transfer the "events" var to the respond?
thanks a lot!!!

Techwraith

unread,
Nov 8, 2012, 8:49:39 PM11/8/12
to ged...@googlegroups.com
You'll need to make sure that your variables are all in the scope that you need them to be in:

this.index= function(req, resp, params){

 
var self = this;

  geddy
.model.Event.getFutureEvents(function(err, events) {
    geddy
.model.Article.all({}, {sort: {createdAt: 'desc'}}, function(err, articles) {

     
self.respond({params: params, articles: articles, events: events});
   
});

 
});

}

Eric Ponce

unread,
Nov 9, 2012, 5:27:05 AM11/9/12
to ged...@googlegroups.com
Thanks! it is what I thinking,....Now its working on 100%
But, Imagine that I want to pass a lot of data. For example:

geddy.model.Event.getFutureAvents(function(err, a) {
  geddy.model.Some.getFutureBvents(function(err, b) {
    geddy.model.Thing.getFutureCvents(function(err, c) {
      geddy.model.Mouse.getFutureDvents(function(err, d) {
        geddy.model.Keyboard.getFutureEEvents(function(err, e) {
          geddy
.model.Ipod.getFutureFvents(function(err, f) {

            geddy
.model.Article.all({}, {sort: {createdAt: 'desc'}}, function(err, articles) {

             
self.respond({params: params, articles: articles, events: events, a:a,b:b,c:c,d:d.....[etc]});
           
});
         
});
        });
      });
    });
  });
});

This is the best method to do this?
thanks :D

Techwraith

unread,
Nov 9, 2012, 4:22:59 PM11/9/12
to ged...@googlegroups.com
So this is where I would start using named functions instead of nested anonymous functions:

var self = this
 
, opts = {};

opts
.params = params;

var gotThings = function (err, things) {
  opts
.things = things;
 
this.respond(opts);
}

var gotSomes = function (err, somes) {
  opts
.somes = somes;
  geddy
.model.Thing.getFutureThings(gotThings);
}

var gotEvents = function (err, events) {
  opts
.events = events
  geddy
.model.Some.getFutureSomes(gotSomes);
}

Does that make sense?

Eric Ponce

unread,
Nov 11, 2012, 8:58:52 PM11/11/12
to ged...@googlegroups.com
Oh nice! Thanks for this example. It's perfect!! ;)
Reply all
Reply to author
Forward
0 new messages