Show different objects in order

21 views
Skip to first unread message

Eric Ponce

unread,
May 13, 2013, 7:13:37 PM5/13/13
to ged...@googlegroups.com
Hello people!

I have 3 models. For example, songs, status and photos. Each object has a particular structure:

Song:
id, title, body
Status
id, text
Photos
id, title, fileName

One user hasMany the 3 objects: hasMany(Songs), hasMany(Status), hasMany(Photos)

How can I show MY objects in my profile in order of updateDate, for example. This would be the result:

My profile:

Song2 - updateDate(5/1/2013 19:00)
Title: One song2
Body: The body song2
-----------
Song - updateDate(4/1/2013 00:00)
Title: One song
Body: The body song
-----------
Status - updateDate(3/1/2013 03:00)
Text: Hello world
-----------
Photo - updateDate(2/1/2013 10:00)
Title: One pic
fileName: the_pic.jpg
-----------
Status - updateDate(1/1/2013 00:00)
Text: My first object
 
I can give all objects in the controller but I don't know how can I intercalate different objects with a "for" ore some similar loop.
Show the same object its very easy with "for( var i in songs)" but with different objects I don't know how can I do it...

thanks to all!

Matthew Eernisse

unread,
May 14, 2013, 1:41:08 AM5/14/13
to ged...@googlegroups.com
I think this is what you want: https://github.com/mde/model#sorting-results


--
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.
 
 

Eric Ponce

unread,
May 14, 2013, 5:25:54 AM5/14/13
to ged...@googlegroups.com
I'm not sure...

First of all, I'm using MongoDB

And the other thing, If I needed to do this, i would do some like this:

geddy.model.User.first( someId, function(err, user){
    user.getSongs(function(err, songs){
    user.getPhotos(function(err, photos){
    user.getStatus(function(err, status){
        self.respond(songs, photos, status);
    });
    });
    });
});

And in the view:

for( var i in songs ){
}
for( var p in photos ){
}
for( var s in status ){
}

but the results will not be intercaled.
Doing this the result will be:

Song
Song2
---
Photo
Photo2
Photo3
---
Status

And I want:

Song
Photo
Photo2
Status
Photo3
Song2

Ordered by createdAt, updatedAt or id, it doesn't matter....its an example...
thanks matthew and sorry

Eric Ponce

unread,
May 16, 2013, 11:28:51 AM5/16/13
to ged...@googlegroups.com
I'ts possible to do this "easily" or I have to get all objects, put in array and order them, or similar...?
thanks ;)

Matthew Eernisse

unread,
May 17, 2013, 12:15:37 AM5/17/13
to ged...@googlegroups.com
Yes, if you're using Mongo, sort is only sorted on properties on the actual object, not items inside collection properties. You'll have pass those properties to a `sort` function with a specific comparator function that sorts them into the order you want: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort
Reply all
Reply to author
Forward
0 new messages