Skip to content

Instantly share code, notes, and snippets.

@ardok
Created September 11, 2012 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardok/3702924 to your computer and use it in GitHub Desktop.
Save ardok/3702924 to your computer and use it in GitHub Desktop.
These are docs for JS Tutorial

Objective

Create and save a new User object.

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Create a User Object

var user = new StackMob.User({ username: 'Bill Watterson', password: 'weirdosfromanotherplanet', profession: 'cartoonist'  });
user.create();

Callback

Remember that we can add callbacks to our API call! Take our example above:
user.create({
	success: function(model) {
	  console.debug('User object is saved, username: ' + model.get('username'));
	},
	error: function(model, response) {
	  // response.error is the container for our error message
	  console.debug("Aww...why did you fail on me?! " + response['error']);
	}
});

Objective

Create and save a new object.

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob JS SDK.

Create a Todo Object

If you want to create an object other than user (following your schema name), we need to modify the code a little bit. Say we want to create an object of todo (this is our schema name).

Note: StackMob will create the schema for you if you haven't created it yet.

var Todo = StackMob.Model.extend({ schemaName: 'todo' });
var myTodo = new Todo({ title: 'Send Data to StackMob!' });
myTodo.create();

Add More Fields

If you want to add more fields to your object, all you need to do is add more map keys.
var Todo = StackMob.Model.extend({ schemaName: 'todo' });
// completed will be a boolean field if it's not created already
var myTodo = new Todo({ title: 'Send Data to StackMob!', priority: 1, completed: true });
myTodo.create();

Callback

Remember that we can add callbacks to our API call! Take our example above:
myTodo.create({
	success: function(model) {
		console.debug('Todo object is saved, todo_id: ' + model.get('todo_id') + ', title: ' + model.get('title'));
	},
	error: function(model, response) {
		// response.error is the container for our error message
		console.debug("Aww...why did you fail on me?! " + response['error']);
	}
});

Objectives

Delete a User Object

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Delete a User Object

Let's try to delete a user whose name is Bill Watterson

var user = new StackMob.User({ username: 'Bill Watterson' });
user.destroy();

Callback

Remember that we can add callbacks to our API call! Take our example above:
user.destroy({
	success: function(model) {
		console.debug('User object is destroyed, username: ' + model.get('username'));
	},
	error: function(model, response) {
		// response.error is the container for our error message
		console.debug("Aww...why did you fail on me?! " + response['error']);
	}
});

Objectives

Delete an Object

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Delete an Object

Let's try to delete a todo object which id is todo1. Remember that we can only delete an object with its primary key (id).

var Todo = StackMob.Model.extend({ schemaName: 'todo' });
var myTodo = new Todo({ todo_id: 'todo1'});
myTodo.destroy({
	success: function(model) {
		console.debug(model.toJSON());
	},
	error: function(model, response) {
		// response['error'] is the container for our error message
		console.debug("Aww...why did you fail on me?! " + response['error']);
	}
});

Objectives

Delete Objects Without Knowing Their IDs

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

  • Running StackMob Python Web Server
  • JS SDK is initialized

Don't know how to run Web Server or initialize the JS SDK?

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Delete an Object

Let's try to delete todo objects which IDs we don't know.

We want to delete those todo objects which title is Send Data to StackMob!.

Steps to do this:

  • Read only those todo objects with Send Data to StackMob! as their title field.
  • Get the todo_id field to delete each object using destroy.
<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://static.stackmob.com/js/json2-min.js"></script>
    <script type="text/javascript" src="http://static.stackmob.com/js/underscore-1.3.3-min.js"></script>
    <script type="text/javascript" src="http://static.stackmob.com/js/backbone-0.9.2-min.js"></script>
    <script type="text/javascript" src="http://static.stackmob.com/js/stackmob-js-0.5.5-min.js"></script>
 
    <script type="text/javascript">
    /* <![CDATA[ */
        StackMob.init({
            appName: 'your_app_name',
            clientSubdomain: 'your_client_subdomain',
            publicKey: 'your_public_key',
            apiVersion: 0
        });
    /* ]]> */
    </script>
  </head>
  <body>
 
  <script type="text/javascript">
	var Todo = StackMob.Collection.extend({ schemaName: 'todo' });
	var myTodo = new Todo();
	var q = new StackMob.Collection.Query();
	q.equals('title', 'Send Data to StackMob!');
	myTodo.query(q, {
		success: function(model) {
			// Successfully fetched all the objects which title is 'Send Data to StackMob!'
			var modelFetched = model.toJSON();
			
			// Get the size of our objects
			var size = _.size(model.toJSON());
			
			if (size === 0) console.log('There is no object to delete');
			else {
				// Counter to iterate through our objects
				var i = 0;
				
				// Counter to count the number of deleted objects
				var objectsDeleted = 0;
				
				for (i = 0; i < size; ++i) {
					if (modelFetched[i] !== undefined) {
						// Destroy the object if it exists (i.e. not undefined)
						var toDelete = new Todo({ todo_id: modelFetched[i]['todo_id'] });
						toDelete.destroy({
							success: function(model) {
								// Add our counter as we successfully delete an object
								objectsDeleted = objectsDeleted + 1;
								if (objectsDeleted == size) {
									// do something when the last object to delete has been deleted
									console.log('All ' + size + ' object(s) have been deleted');
								}
							},
							error: function(model, response) {
								console.log(response['error']);
							}
						})
					}
				}
			}
		},
		error: function(model, response) {
			// response['error'] is the container for our error message
			console.debug("Aww...why did you fail on me?! " + response['error']);
		}
	});
  </script>
 
  </body>
</html>

Objectives

Read a Specific Object

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Read a Specific Object

Let's try to read specific Todo object which title is `Send Data to StackMob!`.
var Todo = StackMob.Model.extend({ schemaName: 'todo' });
var Todos = StackMob.Collection.extend({ model: Todo });
var myTodos = new Todos();
var q = new StackMob.Collection.Query();
q.equals('title', 'Send Data to StackMob!');
myTodos.query(q, {
	success: function(model) {
		console.debug(model.toJSON());
	},
	error: function(model, response) {
		// response['error'] is the container for our error message
		console.debug("Aww...why did you fail on me?! " + response['error']);
	}
});

Objectives

Read a User object

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Read a User Object

var user = new StackMob.User({ username: 'Bill Watterson' });
user.fetch({
	success: function(model) {
	console.debug(model.get('username') + ': ' + model.get('profession'));
	}
});

Objectives

Read all Todo Objects

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Read All Todo Objects

Assuming you have created a few Todo objects, let's fetch all of them!

var Todo = StackMob.Model.extend({ schemaName: 'todo' });
var myTodo = new Todo();
myTodo.fetch({
	success: function(model) {
		console.debug(model.toJSON());
	},
	error: function(model, response) {
		// response['error'] is the container for our error message
		console.debug("Aww...why did you fail on me?! " + response['error']);
	}
});
</html>

Objective

Update a User Object

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Update a User Object

Here is the simplest way to update a User object

// if there is a field that is not there yet, StackMob will automatically create it for you
var user = new StackMob.User({ username: 'Bill Watterson', profession: 'professional clown', age: 20 });
user.save({}, {
	success: function(model) {
		console.debug(model.toJSON());
	},
	error: function(model, response) {
		console.log(response['error']);
	}
});

We can also put the fields that we want to update inside .save().

var user = new StackMob.User({ username: 'Bill Watterson' });
user.save({ profession: 'consultant', age: 30}, {
	success: function(model) {
		console.log(model.toJSON());
	},
	error: function(model, response) {
		console.log(response['error']);
	}
});

Another way is to set the fields locally

var user = new StackMob.User({ username: 'Bill Watterson' });
user.set({ profession: 'gogo dancer', age: 21 });
user.save({}, {
	success: function(model) {
		console.log(model.toJSON());
	},
	error: function(model, response) {
		console.log(response['error']);
	}
});

Objective

Update a Todo Object

Experience Level

Beginner

Estimated Time to Complete

~5 minutes

Prerequisites

* Running the StackMob Python Web Server with your initialized JS SDK

Let's get started!

This assumes that you have properly put in all the required scripts and have initialized StackMob.

Update a Todo Object

Here is how we are going to update a todo object which id is todo1. We are updating its title field to become "try StackMob"

var Todo = StackMob.Model.extend({ schemaName: 'todo' });
var myTodo = new Todo({ todo_id: 'todo1', title: 'try StackMob' });
myTodo.save({}, {
	success: function(model) {
		console.debug(model.toJSON());
	},
	error: function(model, response) {
		console.log(response['error']);
	}
});

We can also put the fields that we want to update inside .save().

var Todo = StackMob.Model.extend({ schemaName: 'todo' });
var myTodo = new Todo({ todo_id: 'todo1' });
myTodo.save({ title: 'try StackMob' }, {
	success: function(model) {
		console.debug(model.toJSON());
	},
	error: function(model, response) {
		console.log(response['error']);
	}
});

Another way is to set the fields locally

var Todo = StackMob.Model.extend({ schemaName: 'todo' });
var myTodo = new Todo({ todo_id: 'todo1' });
myTodo.set({ title: 'try StackMob' });
myTodo.save({}, {
	success: function(model) {
		console.debug(model.toJSON());
	},
	error: function(model, response) {
		console.log(response['error']);
	}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment