Pergunta

I'm building a food ordering system in my app (html 5 sdk). When creating item, an item can have multiple toppings (Extra Cheese, extra chicken etc).

So this is what I wanna do

  1. Insert item into the Items Table (I can simply do it)
  2. Get the Array of toppings sent from app and insert it into Toppings Table (NOt sure how to send the array of toppings. Each topping will have a name and price) Can I add them as an attribute to items insert service request and send??

Problem

I can insert an item without problem, but I have no idea how to get the array list of toppings and insert it into Topping tables.

Can I send array of objects as an attribute in the Azure mobile services service request?

Thanks for your time in advance.

Foi útil?

Solução

One way to deal with this is to have a custom insert script on your Items table so that it parses an array and inserts each item of the array into a Toppings table (including item id, if you need that). For example:

function insert(item, user, request) {
    var toppings = item.Toppings;
    item.Toppings = null;
    request.execute({ success: function() {
        var toppingsTable = tables.getTable('Toppings');
        var count = 0;
        toppings.forEach(function(topping, index) {
            topping.itemId = item.id;
            toppingsTable.insert(topping, {
                success: function() {
                    count++;
                    if (toppings.length === count) {
                        request.respond();
                    }
                },
                error: function(err) {
                    console.warn('Error while inserting toppings objects', err);
                    count++;
                    if (toppings.length === count) {
                        request.respond();
                    }
            });
        }});
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top