Accessing data from device

The namespace includes APIs that work with any cloud data providers configured for the Supersonic application. They provide efficient ways for manipulating data – adding, updating and deleting records.

In the following we demonstrate how supersonic data makes it joyfully easy to manipulate resources in the defined backend database (such as AppGyver sandbox DB) in a clean, object-oriented way.

Code snippets in below assume that the application has a resource Beer with two string fields name and brewery, and an integer field brewed_since configured.

Once a resource has been defined, app can get hold of it with method which takes the resource name as parameter:

var Beer ='Beer');
Beer ='Beer')

Getting data from the server

The call'Beer') returns a class, that provides methods to accessing the data of the resource.

The entire collection can be achieved by a promise returning function findAll of the Model class:

// all the below examples assume that variable Beer has been created
var Beer ='Beer')

Beer.findAll().then( function(allBeers) {
  // access here the collection allBeers
# all the below examples assume that variable Beer has been created
Beer ='Beer')

Beer.findAll().then (allBeers) ->
  # access here the collection allBeers

The returned object allBeers is of type that acts like an array:

Beer.findAll().then( function(allBeers) {
  console.log("the first was " + allBeers[0].name);

  for (i = 0; i < allBeers.length; i++) {
    console.log(allBeers[i].name + " by " + allBeers[i].brewery + " brewed since " + allBeers[i].brewed_since);
Beer.findAll().then (allBeers) ->
  console.log "the first was #{allBeers[0].name}"

  for beer in allBeers
    console.log "#{} by #{beer.brewery} brewed since #{beer.brewed_since}"

Objects in the collection are instances of the class

Three-way Data Binding

The most awesome feature of Supersonic data is it's ability to listen if the data has changed at the backend server. Listening the changes is done with method all:

Beer.all().whenChanged( function(beers) {
  console.log("the state of the beers has changed, current ");
  for (i = 0; i < beers.length; i++) {
    console.log(beers[i].name + " by " + beers[i].brewery + " brewed since " + beers[i].brewed_since);
Beer.all().whenChanged (beers) ->
  console.log "the state of the beers has changed, current "
  for beer in beers
    console.log "#{} by #{beer.brewery} brewed since #{beer.brewed_since}"

Any time there are changes in data, the registered callback function is called. So with Angular's databinding and magical touch of Supersonic data one can easily build applications where the data of all the users is kept in sync all the time!

See more from the API.

Model instances

Method find in the Model class can be used to get a single object based on the identifier field:

Beer.find("123").then( function(beer) {
  console.log("beer with id 123 is " + + " by " + beer.brewery);
Beer.find("123").then (beer) ->
  console.log "beer with id 123 is #{} by #{beer.brewery}"

The returned objects are instances of

A model instance can be edited and the edited version saved back to the cloud database:

Beer.find("123").then( function(beer) {
  beer.brewery = "Sierra Nevada Brewing"; function() {
    console.log("beer saved");
Beer.find("123").then (beer) ->
  beer.brewery = "Sierra Nevada Brewing" () ->
    console.log "beer saved"

It is also possible to delete model instances:

Beer.find("555").then( function(beer) {
  beer.delete().then( function() {
    console.log("a crappy beer was deleted");
Beer.find("555").then (beer) ->
  beer.delete().then () ->
    console.log "a crappy beer was deleted"

Creating new model instances

Create and save new objects to database is easy:

var object = {
  name: "Arctic Cirle Ale",
  brewery: "Malmgard",
  brewed_since: 2009,

var beer = new Beer(object); function() {
  console.log("beer created!");
object =
  name: "Arctic Cirle Ale"
  brewery: "Malmgard"
  brewed_since: 2009

beer = new Beer(object) () ->
  console.log "beer created!"

Once the method save has been called to the Model instance, the new object is saved to the backing database. And thanks to three way data binding, all the apps who have registered for the changes in collection Beers will get updated with the new data!

Further reading

Learn more about from the API docs.