Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active December 29, 2015 13:49
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 MotiurRahman/7680203 to your computer and use it in GitHub Desktop.
Save MotiurRahman/7680203 to your computer and use it in GitHub Desktop.
Android and IOS: Remote database synchronization with CommonJS Module. Insert update delete locally as well as remotely
// Insert Update Delete for ACS Here
var Cloud = require('ti.cloud');
exports.add = function(text, details, callback) {
Cloud.Objects.create({
classname : 'info',
fields : {
title : text,
description : details
}
}, function(e) {
if (e.success) {
var info = e.info[0];
//alert('Successfully add data to the remote ACS');
callback(info.id);
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
};
exports.update = function(text, details, id) {
Cloud.Objects.update({
classname : 'info',
id : id,
fields : {
title : text,
description : details
}
}, function(e) {
if (e.success) {
var info = e.info[0];
//alert('Successfully update data to the remote ACS');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
};
exports.remove = function(cu_id) {
Cloud.Objects.remove({
classname : 'info',
id : cu_id
}, function(e) {
if (e.success) {
//alert('Successfully delete data to the remote ACS');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
};
/*1. I Create a simple apps
2. Here a simple database table with 3 fields [id, title and descriptions]
3. In root window will display data from database
4. Here Added edit and delete options in each row, user’s can edit/delete data from database table
5. Add database synchronizer to remote web server (such as ACS) We can create user and insert
data, update data, delete data to the Wev server as well as local database.
6. When user’s close this apps, local database will synchronize with remote database */
var Cloud = require('ti.cloud');
Cloud.Users.create({
email :'motiur.mbstu@gmail.com',
first_name :'motiur',
last_name : 'rahman',
password : '1234',
password_confirmation : '1234',
}, function(e) {
if (e.success) {
var user = e.users[0];
// alert('Successfully created User to te Remote ACS');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
Cloud.Users.login({
login : 'motiur.mbstu@gmail.com',
password : '1234'
}, function(e) {
if (e.success) {
var user = e.users[0];
var MasterView = require('ui/common/MasterView');
var masterView = MasterView();
masterView.open();
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
// update database work create here
exports.edit = function(title, details, id) {
var mainView = Ti.UI.createView({
width : Ti.UI.FILL,
height : 200,
layout : 'vertical'
});
var titleData = Ti.UI.createTextField({
right : 10,
left : 10,
top : 5,
height : Ti.UI.SIZE,
softKeyboardOnFocus : Ti.UI.Android.SOFT_KEYBOARD_DEFAULT_ON_FOCUS, // Android only
keyboardType : Titanium.UI.KEYBOARD_ASCII,
returnKeyType : Ti.UI.RETURNKEY_NEXT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
mainView.add(titleData);
var description = Ti.UI.createTextField({
top : 10,
right : 10,
left : 10,
bottom : 5,
height : Ti.UI.SIZE,
softKeyboardOnFocus : Ti.UI.Android.SOFT_KEYBOARD_DEFAULT_ON_FOCUS, // Android only
keyboardType : Titanium.UI.KEYBOARD_ASCII,
returnKeyType : Ti.UI.RETURNKEY_NEXT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
mainView.add(description);
var dialog = Ti.UI.createAlertDialog({
title : 'Information Update',
buttonNames : ['Update', "Cancel"],
androidView : mainView
});
titleData.setValue(title);
description.setValue(details);
dialog.addEventListener('click', function(e) {
if (e.index == 0) {
var db = require('lib/db');
var ACS = require('lib/acs');
ACS.update(titleData.value, description.value, id);
db.updateinfo(titleData.value, description.value, id);
Ti.App.fireEvent('tableUpdate');
}
});
dialog.show();
};
// Insert Update Delete in Database create here
var db = Ti.Database.open('people');
db.execute('CREATE TABLE IF NOT EXISTS customer(id integer PRIMARY KEY autoincrement not null,customer_id TEXT,title TEXT,description TEXT)');
db.close();
exports.add = function(customer_id,title, description) {
var db = Ti.Database.open('people');
db.execute('INSERT INTO customer (customer_id,title,description) VALUES(?,?,?)', customer_id,title,description);
db.close();
};
exports.getinfo = function() {
var customerInfo = [];
var db = Ti.Database.open('people');
var result = db.execute('select * from customer');
while (result.isValidRow()) {
customerInfo.push({
id : result.fieldByName('id'),
title : result.fieldByName('title'),
description:result.fieldByName('description'),
customer_id:result.fieldByName('customer_id'),
});
result.next();
}
result.close();
db.close();
//Ti.API.info('stuInfo'+ stuInfo);
return customerInfo;
};
exports.updateinfo = function(title,description,customer_id) {
var db = Ti.Database.open('people');
db.execute('UPDATE customer set title=?,description=? where customer_id=?', title,description,customer_id);
db.close();
};
exports.deletinfo = function(customer_id) {
var db = Ti.Database.open('people');
db.execute('DELETE FROM customer where customer_id=?', customer_id);
db.close();
};
// This is the main View of show Table data from database.
function MasterView() {
var win = Ti.UI.createWindow({
backgroundImage : "/image/green.jpg",
layout : 'vertical',
navBarHidden : true,
fullscreen : true
});
var Cloud = require('ti.cloud');
//Top View
var view = Ti.UI.createView({
backgroundColor : 'red',
width : Titanium.UI.FILL,
height : 50,
top : 0,
layout : 'horizontal'
});
win.add(view);
var textName = Ti.UI.createLabel({
text : 'Database Synchronization',
color : '#FFF',
font : {
fontSize : 25,
fontFamily : 'AlexBrush-Regular'
},
left : 5
});
view.add(textName);
//Button Bar View
var saveView = Ti.UI.createView({
width : Titanium.UI.FILL,
height : Titanium.UI.SIZE,
top : 0
});
// Create a Button.
var save = Ti.UI.createButton({
title : 'save',
height : 60,
width : 90,
left : 10
});
saveView.add(save);
var textArea = Ti.UI.createTextArea({
height : Titanium.UI.SIZE,
top : 0,
color : 'red',
width : Titanium.UI.FILL,
hintText : 'Add Title',
softKeyboardOnFocus : Ti.UI.Android.SOFT_KEYBOARD_DEFAULT_ON_FOCUS, // Android only
keyboardType : Ti.UI.KEYBOARD_DEFAULT,
returnKeyType : Ti.UI.RETURNKEY_DEFAULT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(textArea);
var details = Ti.UI.createTextArea({
height : Titanium.UI.SIZE,
top : 10,
color : 'red',
width : Titanium.UI.FILL,
hintText : 'Add Description',
softKeyboardOnFocus : Ti.UI.Android.SOFT_KEYBOARD_DEFAULT_ON_FOCUS, // Android only
keyboardType : Ti.UI.KEYBOARD_DEFAULT,
returnKeyType : Ti.UI.RETURNKEY_DEFAULT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(details);
//Listener for save
save.addEventListener('click', function() {
if (textArea.value == '') {
alert('please write something');
} else {
var title = textArea.value;
var detailsValue = details.value;
var DB = require('lib/db');
textArea.setValue('');
details.setValue('');
var ACS = require('lib/acs');
ACS.add(title, detailsValue, function(callback) {
//Ti.App.Properties.setString('customer_id', callback);
DB.add(callback, title, detailsValue);
Ti.App.fireEvent('tableUpdate');
});
}
textArea.blur();
});
win.add(saveView);
// Create a TableView.
var tableData = [];
var tableView = Ti.UI.createTableView({
data : tableData,
top : 0,
});
Ti.App.addEventListener('tableUpdate', refresh);
refresh();
function refresh() {
tableData = [];
var db = require('lib/db');
var infoData = db.getinfo();
for (var i = 0; i < infoData.length; i++) {
var row = Ti.UI.createTableViewRow({
className : 'forumEvent', // used to improve table performance
selectedBackgroundColor : 'white',
rowIndex : i, // custom property, useful for determining the row during events
height : 'auto',
title : infoData[i].title,
id : infoData[i].id,
details : infoData[i].description,
customer_id : infoData[i].customer_id
});
//alert(infoData[i].id);
var id = Ti.UI.createLabel({
color : 'red',
text : infoData[i].id,
font : {
fontSize : 25,
},
top : 2,
left : 10,
right : 75
});
row.add(id);
var title = Ti.UI.createLabel({
color : 'red',
font : {
fontFamily : 'Arial',
fontSize : 25,
fontWeight : 'normal'
},
text : infoData[i].title,
top : 30,
left : 10,
right : 75
});
row.add(title);
var discription = Ti.UI.createLabel({
color : 'red',
font : {
fontFamily : 'Arial',
fontSize : 25,
fontWeight : 'normal'
},
text : infoData[i].description,
top : 60,
left : 10,
right : 60
});
row.add(discription);
var del = Ti.UI.createButton({
title : 'Del',
right : 2,
height : 40,
id : "delrow",
width : 50,
myrow : row
});
row.add(del);
tableData.push(row);
}
tableView.setData(tableData);
}
tableView.addEventListener('click', function(e) {
// Working Here for Delete date
if (e.source.id == "delrow") {
var customer_id = e.rowData.customer_id;
Ti.App.Properties.setString('customer_id', customer_id);
//alert('id='+newData);
var dialog = Ti.UI.createAlertDialog({
cancel : 1,
buttonNames : ['Confirm', 'Cancel'],
message : 'Would you like to delete the file?',
title : 'Warning'
});
dialog.addEventListener('click', function(e) {
if (e.index === e.source.cancel) {
} else {
// tableView.deleteRow(e.source.myrow);
var db = require('lib/db');
//alert(e.source);
var cu_id = Ti.App.Properties.getString('customer_id');
db.deletinfo(cu_id);
Ti.App.fireEvent('tableUpdate');
var ACS = require('lib/acs');
ACS.remove(cu_id);
}
});
dialog.show();
}
// working here for update data
else {
var dataEditt = require('ui/common/dataEdit');
dataEditt.edit(e.rowData.title, e.rowData.details, e.rowData.customer_id);
}
});
win.add(tableView);
return win;
}
module.exports = MasterView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment