Freigeben über


Using Parameters with Tables for Different Views – Windows Azure Mobile Services

In my two previous posts I discussed how to move your table filter logic server side, and how to use Virtual tables to get different views of data.  In this blog post I will show you how to pass parameters to you read script to get different views of the data.

In this example, the default view of this table will be to filter the complete items (since my services all require this as a default view) and still expose a way to get the unfiltered data back if I wish.  This will also preserve the query and select parameters passed to the read in the event I wish to use those!

Server Script

The server script will use a jump table to execute a code path based on the parameter passed in.  It also has stubbed out another operation to show how this would be done.

 function read(query, user, request) {
    //console.log(request.parameters.operation);
    var dispatch = {
        op1: operation1,
        nofilters: operation2,
    }

    //doesn't return valid JSON but shows how you might 
    // execute another function based on different parameters passed in
    function operation1(query, user, request) {
        request.respond(200, "this result is from operation1");
    }

    // no server side filter applied 
    function operation2(query, user, request) {
        request.execute();
    }

    // if we have parameters and a matching entry in the jump table we have defined
    // dispatch it!
    if (request.parameters.operation && dispatch.hasOwnProperty(request.parameters.operation)) {
        dispatch[request.parameters.operation](query, user, request);
        return;
    }
    else // otherwise execute the default logic I have defined.
    {

        query.where(function () {
            return this.complete === false && this.createdAt !== null;
        }
        );


        request.execute({
            success: function (results) {
                results.forEach(function (item) {
                    delete item.createdAt;
                }

                );
                request.respond();
            }
        }

        );
    }
}

 

Client Code:

     private async Task<string> RefreshTodoItems()
        {
            string aStr = "MyString";
            MobileServiceInvalidOperationException exception = null;
            try
            {
                // This code refreshes the entries in the list view by querying the TodoItems table.
                // The query excludes completed TodoItems

                Dictionary<string,string> parameters = new Dictionary<string,string>();
                parameters.Add("operation","nofilters");

                items = await todoTable.WithParameters(parameters).ToCollectionAsync();
            }
            catch (MobileServiceInvalidOperationException e)
            {
                exception = e;
            }

            if (exception != null)
            {
                await new MessageDialog(exception.Message, "Error loading items").ShowAsync();
            }
            else
            {
                ListItems.ItemsSource = items;
            }
            return aStr;
        }

 

You should be able to see how you could extend this to do things like execute a SQL statement passed in as a parameter!

Let me know if you find this useful,

Jeff