locked
Update server side when record is read RRS feed

  • Question

  • Hi

    I want to update a Mobile Service field value server side when a record is read client side. Can I use a read script to do this? Say as an example I have a field called iCount, then  iCount = iCount + 1.

    How to modify this to do that?

    function read(query, user, request) {
    
        request.execute();
    
    }

    Saturday, February 8, 2014 12:18 AM

Answers

  • Do you want to update the counter every time an item is read from the database? If the app reads 200 records, then yes, the server script will cause 200 update calls to be made. Do you want to update the count only if an action is done on the client side (i.e., a click)? Then calling the client.Update[Async] at that location is an option. If you want users to only increase the read count for items then "own", then you can filter the requests based on the user id (assuming they're authenticated), and at this point the items read will only belong to an individual user. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/14/storing-per-user-data-in-azure-mobile-services.aspx shows one way to split the data based on the user.

    Carlos Figueira

    • Marked as answer by Andy Sid Thursday, March 13, 2014 3:13 AM
    Saturday, February 8, 2014 1:27 AM

All replies

  • You can certainly do that. You'd have to iterate through the results to be returned in the query and call "table.update" with the updated value. The code would look something like the one below:

    function read(query, user, request) {
        request.execute({
            success: function(results) {
                var toUpdate = results.length;
                var table = tables.current;
                results.forEach(function(result) {
                    result.iCount = 1 + result.iCount;
                    table.update(result, {
                        success: function() {
                            toUpdate--;
                            if (toUpdate === 0) {
                                request.respond(200, results);
                            }
                        }
                    });
                });
            }
        });
    }


    Carlos Figueira

    Saturday, February 8, 2014 1:09 AM
  • Thanks Carlos

    It occurred to me that my App takes 200 records when loading a gridview. Using a server side script would cause all of those to be updated? I can use a client side to update the count from a click event, but that would only be fired from the most recent App release, so not reflect the reads from other users. not sure if there is another way to go about this.

    Saturday, February 8, 2014 1:17 AM
  • Do you want to update the counter every time an item is read from the database? If the app reads 200 records, then yes, the server script will cause 200 update calls to be made. Do you want to update the count only if an action is done on the client side (i.e., a click)? Then calling the client.Update[Async] at that location is an option. If you want users to only increase the read count for items then "own", then you can filter the requests based on the user id (assuming they're authenticated), and at this point the items read will only belong to an individual user. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/14/storing-per-user-data-in-azure-mobile-services.aspx shows one way to split the data based on the user.

    Carlos Figueira

    • Marked as answer by Andy Sid Thursday, March 13, 2014 3:13 AM
    Saturday, February 8, 2014 1:27 AM