Skip to content

Instantly share code, notes, and snippets.

@ericktai
Created March 14, 2012 00:14
Show Gist options
  • Save ericktai/2032886 to your computer and use it in GitHub Desktop.
Save ericktai/2032886 to your computer and use it in GitHub Desktop.
JS SDK for Relationships
//Define your Weapon object type and tell StackMob to save this type to the "weapon" datastore
var Weapon = StackMob.Model.extend({ schemaName: 'weapon' });
//Create weapons locally in preparation to save them and add them to Chuck Norris
var weapons = [
new Weapon({
name: 'Fists of Fury',
hitpoints: 5, //integer
cost: 0.5, //float
visible: true, //boolean
settings: ['ouch','super ouch','mega ouch'], //array of strings
upgradelevels: [1,2,3], //array of integers
upgradecosts: [5.50,10.05,25.05], //array of floats
enabled: [true, false, true] //array of booleans
}),
new Weapon({
name: 'Beard of Death',
hitpoints: 5000, //integer
cost: 2235.75, //float
visible: true, //boolean
settings: ['ouch','super ouch','mega ouch'], //array of strings
upgradelevels: [1,2,3], //array of integers
upgradecosts: [150.50,1000.50,5250.50], //array of floats
enabled: [true, false, true] //array of booleans
})
];
//Add the weapons to Chuck Norris and create them in the "weapon" datastore at the same time.
var user = new StackMob.User({ username: 'chucknorris' });
user.appendAndCreate('weapons', weapons, { success: function(returnedUser) { console.debug(returnedUser.toJSON(); } } );
/* STACKMOB JS SDK CODE */
//Fetch 'chucknorris' from StackMob. Expand related objects by one level deep
var user = new StackMob.User({ username: 'chucknorris' });
user.fetchExpanded(1, success: function(returnedUser) { console.debug(returnedUser.toJSON()); });
/* RETURNED USER JSON */
//Expanded User. Notice how "weapons" is not an array of IDs, but rather an array of full objects
{
"username": "chucknorris",
"weapons": [
{
"name": "Beard of Death",
"upgradelevels": [
1, 2, 3
],
"visible": true,
"enabled": [
true, false, true
],
"lastmoddate": 1331683848490,
"hitpoints": 5000,
"cost": 2235.75,
"upgradecosts": [
150.5, 1000.5, 5250.5
],
"createddate": 1331683848490,
"weapon_id": "4f5fe2084f8f839d180b440a",
"settings": [
"ouch", "super ouch", "mega ouch"
]
}, {
"name": "Fists of Fury",
"upgradelevels": [
1, 2, 3
],
"visible": true,
"enabled": [
true, false, true
],
"lastmoddate": 1331683848490,
"hitpoints": 5,
"cost": 0.5,
"upgradecosts": [
5.5, 10.05, 25.05
],
"createddate": 1331683848490,
"weapon_id": "4f5fe2084f8f839d180b4409",
"settings": [
"ouch", "super ouch", "mega ouch"
]
}
],
"lastmoddate": 1331683847111,
"age": 50,
"createddate": 1331683847111
}
/* STACKMOB JS SDK CODE */
//Fetch 'chucknorris' from StackMob. Expand related objects by one level deep
var user = new StackMob.User({ username: 'chucknorris' });
user.fetchExpanded(1, success: function(returnedUser) { console.debug(returnedUser.toJSON()); });
/* RETURNED USER JSON */
//Expanded User. Notice how "weapons" is not an array of IDs, but rather an array of full objects
{
"username": "chucknorris",
"weapons": [
{
"name": "Beard of Death",
"upgradelevels": [
1, 2, 3
],
"visible": true,
"enabled": [
true, false, true
],
"lastmoddate": 1331683848490,
"hitpoints": 5000,
"cost": 2235.75,
"upgradecosts": [
150.5, 1000.5, 5250.5
],
"createddate": 1331683848490,
"weapon_id": "4f5fe2084f8f839d180b440a",
"settings": [
"ouch", "super ouch", "mega ouch"
]
}, {
"name": "Fists of Fury",
"upgradelevels": [
1, 2, 3
],
"visible": true,
"enabled": [
true, false, true
],
"lastmoddate": 1331683848490,
"hitpoints": 5,
"cost": 0.5,
"upgradecosts": [
5.5, 10.05, 25.05
],
"createddate": 1331683848490,
"weapon_id": "4f5fe2084f8f839d180b4409",
"settings": [
"ouch", "super ouch", "mega ouch"
]
}
],
"lastmoddate": 1331683847111,
"age": 50,
"createddate": 1331683847111
}
/* STACKMOB JS SDK CODE */
//Fetch 'chucknorris' from StackMob. Related objects will be represented by an array of IDs
var user = new StackMob.User({ username: 'chucknorris' });
user.fetch({ success: function(returnedUser) { console.debug(returnedUser.toJSON());} });
/* RETURNED USER JSON */
{
"username": "chucknorris",
"weapons": [
"4f5fe2084f8f839d180b440a", "4f5fe2084f8f839d180b4409"
],
"lastmoddate": 1331683847111,
"age": 50,
"createddate": 1331683847111
}
/* STACKMOB JS SDK CODE */
//Fetch 'chucknorris' from StackMob. Related objects will be represented by an array of IDs
var user = new StackMob.User({ username: 'chucknorris' });
user.fetch({ success: function(returnedUser) { console.debug(returnedUser.toJSON());} });
/* RETURNED USER JSON */
{
"username": "chucknorris",
"weapons": [
"4f5fe2084f8f839d180b440a", "4f5fe2084f8f839d180b4409"
],
"lastmoddate": 1331683847111,
"age": 50,
"createddate": 1331683847111
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment