3

I am using "backendless.com" as a BaaS, what I need is to get data from table. Here I am saving object to table:

function Products(args) {
    args = args || {};
    this.name = args.name || "";
    this.quantity = args.quantity || "";
    this.price = args.price || "";
}


function sendData () {
  var age = document.getElementById("age").value,
    name = document.getElementById("name").value,
    title = document.getElementById("title").value;

  var requestObject = new Products( {
    name: name,
    quantity: quantity,
    price: price,
  });

  var savedRequest = Backendless.Persistence.of( Products ).save( requestObject );
}

This is my index.html file:

<form onsubmit="sendData()">
   <input type="text" placeholder="name" id="name"><br>
   <input type="text" placeholder="quantity" id="quantity"><br>
   <input type="text" placeholder="price" id="price"><br>
   <input type="submit" value="Submit">
</form>

After that, when object was saved to table, I want to call this object, and put it to my table (after clicking submit button): like that

How should I do that?

2 Answers2

2

The result of the following API call:

Backendless.Persistence.of( Products ).save( requestObject )

that is the value you put into the savedRequest variable IS the actual saved object. You should be able to use the object in that variable to render it in your UI.

Mark Piller
  • 1,026
  • 1
  • 7
  • 6
0

You should make an additional request to retrieve the new records and then update the table based on the results. Here is the API documentation on how to retrieve records: https://backendless.com/documentation/data/js/data_basic_search.htm

For example, in your case you may want to have additional function like this and call in right after saving (I'm not a Javascript guy so there may be some mistakes, but the concept should be clear ):

function updateTable() {
  var products = Backendless.Persistence.of( Product ).find(); // here you retrieve the new list of Products
  // now iterate through them
  products.forEach( function( product ) {
    // set your table values to product.name, product.quantity, product.price
  } );
}

By the way, in your particular case you could just manually add the user's values to the table if "save" request succeeds, as long as you don't have any logic attached to table records. But of course, this is not the right solution, because you may end up with an inconsistency between your backend and UI.

Scadge
  • 8,411
  • 2
  • 25
  • 38