Skip to content

Instantly share code, notes, and snippets.

@clathrop
Created August 1, 2012 22:59
Show Gist options
  • Save clathrop/3231420 to your computer and use it in GitHub Desktop.
Save clathrop/3231420 to your computer and use it in GitHub Desktop.
Titanium: Create Custom Objects/Query Custom Objects By Descending Order
var Cloud = require('ti.cloud');
var win = Ti.UI.createWindow({
backgroundColor : 'white',
layout : 'vertical'
});
var sv = Ti.UI.createScrollView({
backgroundColor : '#fff',
contentWidth : 300,
contentHeight : "auto",
showVerticalScrollIndicator : true,
showHorizontalScrollIndicator : true,
borderRadius : 5,
borderWidth : 1,
borderColor : '#999',
top : 10,
left : 10,
right : 10,
bottom : 10,
});
var createUserBtn = Ti.UI.createButton({
title : 'Create User and Sign In',
width : '200dp',
height : '60dp',
top : '10dp'
});
win.add(createUserBtn);
createUserBtn.addEventListener('click', function(e) {
Cloud.Users.create({
username : "testuser1",
password : "testuser1",
password_confirmation : "testuser1",
first_name : "test",
last_name : "user1"
}, function(e) {
if (e.success) {
var user = e.users[0];
alert('Created! You are now logged in as ' + user.id);
} else {
if (e.error && e.message) {
alert('Error :' + e.message);
}
}
});
});
var loginCreateBtn = Ti.UI.createButton({
title : 'Login and Create Objects',
width : '200dp',
height : '60dp',
top : '10dp'
});
win.add(loginCreateBtn);
loginCreateBtn.addEventListener('click', function(evt) {
Cloud.Users.login({
login : 'testuser1',
password : 'testuser1',
}, function(e) {
if (e.success) {
Cloud.Objects.create({
classname : 'books',
fields : {
book_id : 90,
title : 'Javascript',
author : 'Peter'
}
}, function(e) {
if (e.success) {
alert("created and logged in");
} else {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
});
Cloud.Objects.create({
classname : 'books',
fields : {
book_id : 56,
title : 'PHP',
author : 'John'
}
}, function(e) {
if (e.success) {
alert("created and logged in");
} else {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
});
Cloud.Objects.create({
classname : 'books',
fields : {
book_id : 34,
title : 'PHP',
author : 'John'
}
}, function(e) {
if (e.success) {
alert("created and logged in");
} else {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
});
} else {
alert('Login Error:' + ((e.error && e.message) || JSON.stringify(e)));
}
});
});
var queryBtn = Ti.UI.createButton({
title : 'Query By Book#',
width : '200dp',
height : '60dp',
top : '10dp'
});
win.add(queryBtn);
var params = {
classname : "books",
page : 1,
per_page : 10,
order : "-book_id"
};
queryBtn.addEventListener('click', function(e) {
Cloud.Objects.query(params, function(e) {
if (e.success) {
opLabel.text = JSON.stringify(e.books);
} else {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
})
});
var opLabel = Ti.UI.createLabel({
backgroundColor : '#fff',
left : 5,
top : 5,
right : 5,
font : {
fontSize : 25
},
color : '#000'
})
win.add(sv);
sv.add(opLabel);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment