NimbusBase Class Documentation

Nimbus.Auth

Nimbus.Auth is the authorization module. You have to set up your app for authentication first before your data can be written to Dropbox and Google Drive.

setup(sync_services)

This is a method to set up the service, key, secret and app name for the app. We do this so later when your users authenticate with Dropbox or Google Drive, they know what app they are giving access to.

sync_object = { 
    "GDrive": { 
      "key": "{ app key }", 
      "scope": "https://www.googleapis.com/auth/drive", 

      "app_name": "{ app name }" 
    }, 
    "Dropbox": { 
      "key": "{ app key }", 
      "secret": "{ app secret }", 
      "app_name": "{ app name }" 
    },
    "synchronous": true
};

Nimbus.Auth.setup(sync_object); 
				

sync_services : a object containing the app keys and other info necessary to authenticate with each service.

authorized()

Check if the current app is authorized or not- returns true or false.

				Nimbus.Auth.authorized();
				

authorize()

Authorization starts the authorization flow for your current user so he can give the app permission to store data on Dropbox.

For websites, the flow can redirect back to your website after authentication. However, for mobile, you have to have two stage authentication.

				Nimbus.Auth.authorize();
				

authorized_callback()

Authorized callback is an attribute that can be assigned with a callback function to be executed when authentication finishes.

				Nimbus.Auth.authorized_callback = function() { console.log("authentication finished") };
				

Nimbus.Model (Static Methods)

Nimbus.Model is the general model for NimbusBase. The static methods for NimbusBase contains a group of methods for setup, finding models, etc. It is executed on the top model level object.

setup(model_name, array_of_attributes)

This is the method to set up a new model. The parameters are the model name and the attributes, and a model object is returned. There is only one storage type- all the attributes are stored as strings.

				Model_Name = Nimbus.Model.setup("Model Name", [ "attribute 1", "attribute 2", "attribute 3"...]);
				

model_name : a string representing the name of the model.

array_of_attributes : the app key under the Dropbox app info section.

sync_all(callback)

sync_all is an important function. It syncs every instance of a model with the online storage on Dropbox.

A little about how syncing works. Normally, if you add, update or delete some entry while the app is active, the change is written in real time to the data storage in Dropbox.

However, if your app is spread out over multiple devices or on a website, it is possible that the data be changed somewhere else and you would need to sync that change. Sync_all is the function you call for that. You should also call it at the beginning of your app and after your authorize to get all the data.

Syncing is incremental, meaning only data that changes is synced. The entry with the latest timestamp overwrites other entries.

				Model_Name.sync_all( function(){ alert("Everything is synced") } ); 
				

callback : a function that will execute after the data is synced.

find(id)

Finds an object by its id. Returns the object if it's found, throws an unknown record error if it's not found.

				model_instance = Model_name.find(instance_id);
				

id : the id of the object you are looking for. This is a String.

exists(id)

Check if an object exists by it's id. If it exist, true is returned, else false.

				if (Model_name.exists(instance_id)) { console.log("this object exists"); }
				

id : the id of the object your are looking for. This is a String.

findByAttribute(attribute, attribute_value)

Find an object of a model by its attributes. Returns the first object found. Nothing is returned if no object is found.

				instance = Model_Name.findByAttribute("attribute 1", "value 1");
				

attribute : a String representing the attribute you are trying to matching.

attribute_value : a String that's the attribute value you are trying to match.

findAllByAttribute(attribute, attribute_value)

Find an object of a model by its attributes. Returns all the objects found. Nothing is returned if no object is found.

				instance_array = Model_Name.findAllByAttribute("attribute 1", "value 1");
				

attribute : a String representing the attribute you are trying to match.

attribute_value : a String that's the attribute value you are trying to match.

each(callback)

Execute a method on each of the instances in the model. The function that is called should take into it as a parameter an object containing all the data of the instance.

				Model_Name.each(function(data){ console.log(data) });
				

callback : a function of the form function(data){ ...do something with the data }, where data is an instance on the model.

all()

Returns all the instances of a model in an array.

				instance_array = Model_name.all();
				

first()

Get the first instance in a model.

				first = Model_Name.first();
				

last()

Get the last instance in a model.

				last = Model_Name.last();
				

count()

Get the count of all the instances in the model

				count = Model_Name.count();
				

destroyAll()

Delete all the instances in a model.. use with caution.

				Model_Name.destroyAll();
				

Nimbus.Model (Instance Methods)

Here are the methods you call on each instance of an object.

create(key_value_pairs)

Creates a new instance of a model.

				Entry = Nimbus.Model.setup("Entry", ["note", "time"]); 
a = Entry.create({ "note":"hello world", "time":"now" });
				

key_value_pairs : a hashmap containing the key value pairs you want the new instance to have.

save()

Saves the current state of an object, can be used to update the values of objects.

				Entry = Nimbus.Model.setup("Entry", ["note", "time"]); 
a = Entry.create({ "note":"hello world", "time":"now" });
a.note = "now the message is changed"; 
a.save();

model_name : a string representing the name of the model.

array_of_attributes : the app key under the Dropbox app info section.

updateAttribute(key_value_pairs)

Update a bunch of attributes at once.

				Entry = Nimbus.Model.setup("Entry", ["note", "time"]); 
a = Entry.create({ "note":"hello world", "time":"now" });
a.updateAttribute({ "note":"changed", "time":"before" }); 

key_value_pairs : A hashmap containing the key value pairs you want the instance to have.

destroy()

Deletes a model.

				Entry = Nimbus.Model.setup("Entry", ["note", "time"]); 
a = Entry.create({ "note":"hello world", "time":"now" });
a.destroy()
				

attributes

Get the attribute of an object.

				Entry = Nimbus.Model.setup("Entry", ["note", "time"]); 
a = Entry.create({ "note":"hello world", "time":"now" });
console.log( a.note ); //getting the note attribute of the instance
				

Nimbus.Binary (Dropbox Only)

This is the binary portion where you can upload binary files such as images or mp3s. The file is uploaded to Dropbox and a sharelink from Dropbox can be retrieved from Dropbox for the file itself. (Note: This is the newest part of the library, and subject to frequent releases.)

With this also comes a prebuilt object, binary. 'binary' is like all NimbusBase objects- you can call .all(), .first() and other methods with it.

upload_blob(blob, name, callback)

Saves a blob to Dropbox, calls the callback afterwards. If you want to upload a file, upload_file is much better.

Nimbus.Binary.upload_blob(blob, "name.png", function(data){ console.log("return data", data) });

blob : the binary blob

name : the name you want to give to the file

callback : the function that gets called after the save completes. You get back an object with the filename, path, etc.

upload_file(file, callback)

Saves a file object to your Dropbox. The name of the uploaded file will match your current file name.

				var files = document.getElementById('files').files; 
var file = files[0];
Nimbus.Binary.upload_file(file, function(data){ console.log("return data", data) });

file ,the file object

callback : the function that gets called after the save completes. You get back an object with the filename, path, etc.

read_file(binary, callback)

Read a file from the server and return with its contentw.

				a= binary.first();
Nimbus.Binary.read_file(a, function(data){ console.log("the first file", data) });
				

binary ,the binary object

callback : the function that gets called after the save completes. You get back an object with the filename, path, etc.

share_link(binary, callback)

Get the share link for the file. The callback function has a parameter for the returned object, which has the shared url.

a= binary.first();
Nimbus.Binary.share_link(a, function(link_data){ console.log("the share url", link_data.url) }); 

binary ,the binary object

callback : the function that gets called after the save completes. You get back an object with the filename, path, etc.

direct_link(binary, callback)

Get the direct link for the file. The callback function has a parameter for the returned object, which has the shared url.

				a= binary.first();
Nimbus.Binary.direct_link(a, function(link_data){ console.log("the share url", link_data.url) }); 

binary : the binary object

callback : the function that gets called after the save completes. You get back an object with the filename, path, etc.

delete_file(binary)

Delete the file.

a = binary.first() 
Nimbus.Binary.delete_file(a); 

binary : the binary object

Nimbus.Share (GDrive Only)

GDrive brings additional functionalities where a user can share his/her files with other users. This adds powerful features to NimbusBase where you can share the data in your app with other people!

Here's how this works: when someone starts, their app creates a folder in their Google Drive that contains the app model data. Let's call that a "space". They can choose to change the permissions on that space to share the space with other users. If multiple users share their space with one user, then that user can also context switch between the data in the different spaces he has permission to.

The two objects used in the Nimbus.Share:

User Object  
id Google id for the user
name Display name for the user
role Owner if the user is the creator of the space
Writer if the user is invited and has access to the space
pic The link to the user's picture if it exists

Space Object  
id Google id for folder that contains the space
Owner Display name for the user

get_users(callback)

Get a list of the users that have permission to access the current space.

				Nimbus.Share.get_users(function(user_array) { console.log(user_array) })
				

callback : the callback that is executed after you get the result

add_user(email, callback)

Add a user from the space and share the app data.

				Nimbus.Share.add_user("admin@nimbusbase.com", function() { console.log("New user added") })
				

email : the Gmail address of the user you want to add to space

callback : the callback that is executed after you get the result

remove_user(id, callback)

Remove the user from the space.

				Nimbus.Share.remove_user("user_google_id", function() { console.log("User removed") })
				

id : the id of the user you want to remove from the space

callback : the callback that is executed after you get the result

get_me(callback)

Get the user object for the currently logged-in user.

				Nimbus.Share.get_me(function(my_data) { console.log(my_data) })
				

callback : the callback that is executed after you get the result

get_spaces(callback)

Get the spaces that the currently logged-in user has access to.

				Nimbus.Share.get_spaces(function(spaces_array) { console.log(user_array) })
				

callback : the callback that is executed after you get the result