Created
October 2, 2010 23:56
-
-
Save sr3d/608124 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ti.include('../../lib/active_record.js'); | |
Ti.include('../../lib/utils.js'); | |
Ti.include('../../lib/models.js'); | |
var guest = Model.Guest.find(win.guest.id), | |
gift = Model.Gift.find({ first: true, where: {guest_id: guest.id} }), | |
sections = [], | |
currentSection = 0, | |
yesNoBtn, amountInput, giftDescription, | |
tableView = Ti.UI.createTableView({ style: Titanium.UI.iPhone.TableViewStyle.GROUPED }); | |
var doneBtn = Titanium.UI.createButton({ systemButton:Titanium.UI.iPhone.SystemButton.DONE }); | |
doneBtn.addEventListener('click', function(e) { | |
gift = Model.Gift.find({ first: true, where: {guest_id: guest.id} }); | |
if(!gift) { gift = new Model.Gift({guest_id: guest.id}); }; | |
gift.set('description', giftDescription.value ); | |
gift.set('money', amountInput.value ); | |
var parameters = {}; | |
if( yesNoBtn.checked ) { | |
guest.set('gift_received', 'true'); | |
ActiveSupport.extend( parameters, gift.toRailsParamsHash() ); | |
} else { | |
guest.set('gift_received', 'false'); | |
}; | |
ActiveSupport.extend( parameters, { "guest[gift_received]": guest.gift_received } ); | |
// parameters[ 'guest' ] = { }; | |
log(parameters); | |
Request( API_BASE + '/guests/' + guest.id + '/edit_gift.json', { | |
method: 'POST', | |
parameters: ActiveSupport.extend( parameters, { app_token: APP_TOKEN, api_token: API_TOKEN, email: EMAIL } ), | |
onSuccess: function(request) { | |
log(request.responseText); | |
var json = request.responseJSON; | |
log( json ); | |
guest.save(); | |
gift.destroy(); | |
log( json.gift.gift ); | |
if( !json.gift.gift.destroyed ) { | |
gift = Model.Gift.create(Model.Gift.removeUnexistingAttributesFromHash(json.gift.gift)); | |
}; | |
win.close(); | |
}, | |
onError: function(request){ | |
alert('Error saving. Please try again'); | |
} | |
}); | |
}); | |
win.rightNavButton = doneBtn; | |
function buildTableView() { | |
currentSection = 0; | |
sections = []; | |
sections[currentSection] = Ti.UI.createTableViewSection({}); | |
var giftReceivedRow = Ti.UI.createTableViewRow({ title: 'Gift Received?', height: 35, selectionStyle: NO_SELECTION } ); | |
yesNoBtn = Ti.UI.createButton( {width: 99, height: 27, checked: (guest.gift_received == 'true'), right: 10} ); | |
yesNoBtn.addEventListener('click', function(e) { | |
e.source.checked = !e.source.checked; | |
guest.set('gift_received', e.source.checked.toString() ); | |
buildTableView(); | |
}); | |
giftReceivedRow.add(yesNoBtn); | |
sections[currentSection].add(giftReceivedRow); | |
if( guest.gift_received == 'true' ) { | |
currentSection++; | |
var giftDetailsSection = Ti.UI.createTableViewSection({ headerTitle: 'Gift Details' }); | |
sections[currentSection] = giftDetailsSection; | |
var amountRow = Ti.UI.createTableViewRow({ height: 35, selectionStyle: NO_SELECTION } ); | |
amountRow.add( Ti.UI.createLabel( { text: 'Money ($)', color: 'black', left: 10, width: 140, height: 'auto' } ) ); | |
amountInput = Ti.UI.createTextField( { value: (gift.money ? gift.money :''), hintText:'e.g. 12.79', color:'black', height:35 , top: 0, left:140, width: 100, keyboardType:Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION }); | |
amountRow.add(amountInput); | |
giftDetailsSection.add(amountRow); | |
var detailRow = Ti.UI.createTableViewRow({ height: 'auto', selectionStyle: NO_SELECTION } ); | |
detailRow.add( Ti.UI.createLabel( { text: 'Gift Description', color: 'black', left: 10, top: 10, width: 120, height: 'auto' } ) ); | |
giftDescription = Ti.UI.createTextArea( { value: gift.description, color:'black', top: 5, bottom: 5, left:140, width: 140, height: 100, font: {fontSize: 16} }); | |
detailRow.add(giftDescription); | |
giftDetailsSection.add(detailRow); | |
}; | |
/* now init the button after all the sections are initialized */ | |
initButton(); | |
tableView.data = sections; | |
}; | |
function initButton() { | |
yesNoBtn.backgroundImage = yesNoBtn.checked ? '../../images/yes.png' : '../../images/no.png'; | |
}; | |
buildTableView(); | |
win.add(tableView); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment