Home > View Post

Dispatching to different query functions in Mobile Services

It’s common to want to run trigger differing behaviors inside your read script based on a parameter. For example, imagine we have a table called ‘foo’ and we want to have a default path and two special operations called ‘op1’ and ‘op2’ that do something slightly different (maybe one loads a summary of the objects to reduce the amount of traffic on the wire whilst the other expands a relationship to load child records).

Here’s my approach to this:

So now, if I hit the HTTP endpoint for my table

http://todolist.azure-mobile.net/tables/foo

We’ll load the records as normal, returning a JSON array. However, if we add a parameter

http://todolist.azure-mobile.net/tables/foo?operation=op1

Then we’ll get the following response:

"this result is from operation1"

And if we hit ?operation=op2 then we’ll get:

"this result is from operation2"

And, with the script above if we hit some undeclared operation (?operation=nonsense) then we’ll go back to the default path (you may decide to throw an error).

 
Josh Post By Josh Twist
3:18 AM
26 Jan 2013

» Next Post: Debugging your Mobile Service scripts
« Previous Post: Using the scheduler to backup your Mobile Service database

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.