Peter Viglietta
Posts: 0
Joined: Sat Jul 05, 2014 6:22 pm

Querying two collections on one MobileListItem

My app has a built-in messaging system where users can chat with each other. When my users go to into their Messages section, they first get a list of their message threads (chats that they have going with another user). That screen that shows their open chat threads is a Mobile List. On each mobile list item I want to display the Subject, which is stored in the Message Threads collection, and the first 20 characters of the most recent message from that thread, which is stored in the Messages collection. Please see my screen shot for a more detailed walk-through
Image

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Querying two collections on one MobileListItem

Hello Peter,

The idea is basically correct, except the last step.
The scheme looks like this
1) Make the mapping of all the chats in the list, map _id of chat in a hidden field.
2) On success even of service you should hang js code which will be held on all the hidden labels with _id and for each found label it will cause the service to access a collection Messages with the value recorded in hidden label, in request parameter where. You may read more here: https://getsatisfaction.com/apperyio/...?
3) On success event of access service to collection Messages you should hang js code which will look for list item which has hidden label with _id which is the same with _id received in response service (all response with the variable data). In list item that will be find, you should fill the required labels out with data from response parameter

Peter Viglietta
Posts: 0
Joined: Sat Jul 05, 2014 6:22 pm

Querying two collections on one MobileListItem

Hello Evgene,

Thanks for the response. I'm still a little lost. Please see my screenshot below.. I still don't understand how to get the Messages Query Service to run (After the MessageThreads Query Service runs, and puts the MessageThreadId into the hidden label. Please see the screenshot below:

Image

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Querying two collections on one MobileListItem

Peter,

Yes, everything is correct. You should add the js code in mapping in MTIdLabel.
In this js code you should call the service access to Messages. Please read more here:
https://getsatisfaction.com/apperyio/...
The value that you map in the js code is available in the variable value
And, then, item 3. This should help to implement it:
http://api.jquery.com/find/
http://api.jquery.com/text/

Peter Viglietta
Posts: 0
Joined: Sat Jul 05, 2014 6:22 pm

Querying two collections on one MobileListItem

Ok, I think I have some idea of what the code should look like, but I don't know how to write it. Can you take a look at my idea below and let me know if I'm on the right track?

The JS code I should add to the MTIdLabel should be something like this:

service.execute({
and in here I indicate that it's the "Messages Query Service" that I want to execute, that I want it to execute upon success of the "MessageThreads Query Service", and that the request parameter I want to use is that the MessageThreadId is equal to "value"
})

should I be using this sort of code?

service.execute({
data: {
success: function(data, MYPARAMETER) { ... }
}
})

The order is,

1) Runs MessageThreads Query
2) Response to Query puts MessageThreadId in an invisible label called MTIdLabel.
3) MTIdLabel has JS code that does what I just tried to describe above, executes Messages query
4) Response of the Messages query has a response parameter mapped to a visible label on the same list item called Last Message. Here I need to add JS code that looks for every Mobile List item where the id in the hidden label "MTIdLabel" matches the MessageThreadId on the row from the Messages table?

Image

I think I have the right idea? Just need some help with the JS code. Any help is much appreciated!

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Querying two collections on one MobileListItem

Peter,

Yes, that's right. You should try to write the code, we gave you links to jquery above. If you'll have problems please contact us

Peter Viglietta
Posts: 0
Joined: Sat Jul 05, 2014 6:22 pm

Querying two collections on one MobileListItem

So for these parts:

1) Make the mapping of all the chats in the list, map _id of chat in a hidden field.
2) On success even of service you should hang js code which will be held on all the hidden labels with _id and for each found label it will cause the service to access a collection Messages with the value recorded in hidden label, in request parameter where. You may read more here: https://getsatisfaction.com/apperyio/...?

I go into Add JS on the MTIdLabel component, which is the hidden label that will have the IDs

Image

  • The service I want to execute is MessagesQuery

  • The name of the column in the Messages collection that holds the IDs is MessageThreadId

  • if I want to call the MessagesQuery service, how do I indicate that I want all rows where the MessageThreadId is equal to the value of the label?

    is it something like this?

    MessagesQuery.execute({
    data: {
    success: function("MessageThreadId": 'value') { ... }
    }
    })

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Querying two collections on one MobileListItem

Hi Peter,

Should be something like this:prefunction successFunction(data){
//in this function a variable 'element' will point to a label 'MTIdLabel' in needed list item (where you need to add data from 'data' variable)
//i.e. for example you can get needed list item this way $(element).closest("li") and add there needed data, going through response array in 'data' variable
}

MessagesQuery.execute({
data: {
"where": '{"MessageThreadId":"' + value + '"}'
},
success: function(data) {
successFunction(data);
}
});/pre

Peter Viglietta
Posts: 0
Joined: Sat Jul 05, 2014 6:22 pm

Querying two collections on one MobileListItem

Thank you Katya.

I'm not sure I understand correctly, since I'm just learning the fundamentals of JS code.. you're saying that the element variable is created so that after the MessagesQuery service runs, it looks at that Element to know which label to put the data from the Messages collection in? If I want the data from the messages collection to appear in the visible label LastMessage (see my last screenshot), how would I write that?

I'm having trouble figuring out how I would create the element variable.. would it be something like...

function successFunction(data){var element: $(element).closest("li")}

Sorry for my lack of understanding on this, I'm still grappling with the basics of variables and functions. Any help you could give is very much appreciated.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Querying two collections on one MobileListItem

Hello Peter,

Could you please try this:
prevar label = $(element).closest("li").find("[dsid=LastMessage]");
label.text("some new text probably from 'data' variable");/pre

Return to “Issues”