You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 messageconsole.debug("Aww...why did you fail on me?! "+response['error']);}});
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.
varTodo=StackMob.Model.extend({schemaName: 'todo'});varmyTodo=newTodo({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.
varTodo=StackMob.Model.extend({schemaName: 'todo'});// completed will be a boolean field if it's not created alreadyvarmyTodo=newTodo({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 messageconsole.debug("Aww...why did you fail on me?! "+response['error']);}});
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 messageconsole.debug("Aww...why did you fail on me?! "+response['error']);}});
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).
varTodo=StackMob.Model.extend({schemaName: 'todo'});varmyTodo=newTodo({todo_id: 'todo1'});myTodo.destroy({success: function(model){console.debug(model.toJSON());},error: function(model,response){// response['error'] is the container for our error messageconsole.debug("Aww...why did you fail on me?! "+response['error']);}});
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.
<!DOCTYPEhtml><html><head><scripttype="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script><scripttype="text/javascript"src="http://static.stackmob.com/js/json2-min.js"></script><scripttype="text/javascript"src="http://static.stackmob.com/js/underscore-1.3.3-min.js"></script><scripttype="text/javascript"src="http://static.stackmob.com/js/backbone-0.9.2-min.js"></script><scripttype="text/javascript"src="http://static.stackmob.com/js/stackmob-js-0.5.5-min.js"></script><scripttype="text/javascript"> /* <![CDATA[ */
StackMob.init({appName: 'your_app_name',clientSubdomain: 'your_client_subdomain',publicKey: 'your_public_key',apiVersion: 0});
/* ]]>*/</script></head><body><scripttype="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!'varmodelFetched=model.toJSON();// Get the size of our objectsvarsize=_.size(model.toJSON());if(size===0)console.log('There is no object to delete');else{// Counter to iterate through our objectsvari=0;// Counter to count the number of deleted objectsvarobjectsDeleted=0;for(i=0;i<size;++i){if(modelFetched[i]!==undefined){// Destroy the object if it exists (i.e. not undefined)vartoDelete=newTodo({todo_id: modelFetched[i]['todo_id']});toDelete.destroy({success: function(model){// Add our counter as we successfully delete an objectobjectsDeleted=objectsDeleted+1;if(objectsDeleted==size){// do something when the last object to delete has been deletedconsole.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 messageconsole.debug("Aww...why did you fail on me?! "+response['error']);}});
</script></body></html>
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!`.
varTodo=StackMob.Model.extend({schemaName: 'todo'});varTodos=StackMob.Collection.extend({model: Todo});varmyTodos=newTodos();varq=newStackMob.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 messageconsole.debug("Aww...why did you fail on me?! "+response['error']);}});
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!
varTodo=StackMob.Model.extend({schemaName: 'todo'});varmyTodo=newTodo();myTodo.fetch({success: function(model){console.debug(model.toJSON());},error: function(model,response){// response['error'] is the container for our error messageconsole.debug("Aww...why did you fail on me?! "+response['error']);}});</html>
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 youvaruser=newStackMob.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().