Page 1 of 1

Changes of the todo-main js to make it my own for offline functionality

Posted: Wed May 21, 2014 10:08 pm
by Dave Troxel

Please tell me what parts of this script I need to change to use it with my app. Also, does it have to be called todo_main?

thanks

// Constructor of task
function TaskToSynchronize(name, id) {
this.id = id;
this.name = name;
}

// Array of tasks to be deleted when online
var tasksToDelete = [];
// Array of tasks to be created when online
var tasksToCreate = [];

// If there is no network connection
function addTaskToDelete(id, name) {
var tmpTsk = new TaskToSynchronize(name, id);
tasksToDelete.push(JSON.stringify(tmpTsk));
localStorage.setItem('_tasksToDelete', tasksToDelete);
}

function addTaskToCreate(name) {
var tmpTsk = new TaskToSynchronize(name);
tasksToCreate.push(JSON.stringify(tmpTsk));
localStorage.setItem('_tasksToCreate', tasksToCreate);
}

function removeTaskFromCreateList(name, list) {
$(list).each(function(index, Element) {
if (Element.name === name) {
list.splice(index, 1);
}
});
return list;
}

// Start synchronization of tasks with cloud
function startSynchronization() {
var isOnline = localStorage.getItem('_isOnline');
if (isOnline == 1) {
var task;

Code: Select all

     // Deleting 
     var tasks = localStorage.getItem('_tasksToDelete'); 
     if (tasks) { 
         var tasksArr = eval('([' + tasks + '])'); 
         while (tasksArr.length) { 
             task = tasksArr.shift(); 
             if (task.id) { 
                 delete_service.execute({data:{object_id: task.id}}); 
             } 
         } 
         localStorage.setItem('_tasksToDelete', ''); 
     } 

     // Creating 
     tasks = localStorage.getItem('_tasksToCreate'); 
     if (tasks) { 
         var tasksArr = eval('(' + tasks + ')'); 
         while (tasksArr.length) { 
             task = tasksArr.shift(); 
             if (task.name) { 
                 create_service.execute({data:{task: task.name}}); 
             } 
         } 
         localStorage.setItem('_tasksToCreate', ''); 
     } 
 } 
 listServiceExecute(); 

}

// Delete task from list
function removeTaskFromLocalList(id, echo, name) {
if (id) {
$(echo).each(function(index, Element) {
if (Element._id === id) {
echo.splice(index, 1);
}
});
} else if (name) {
$(echo).each(function(index, Element) {
if (Element.task === name) {
echo.splice(index, 1);
}
});
}
return echo;
}

// Add task to the local list
function addTaskToLocalList(name, echo) {
var tmpTask = {};
tmpTask._id = '';

Code: Select all

 tmpTask.task = name; 
 echo.push(tmpTask); 
 return echo; 

}

// Check connection
function checkConnection() {
if (navigator && navigator.network && navigator.network.connection && navigator.network.connection.type) {
var networkState = navigator.network.connection.type;

Code: Select all

     if (networkState !== Connection.NONE) { 
         onOnline(); 
     } else { 
         onOffline(); 
     } 
     // When debug only (in desktop browser) 
 } else { 
     onOnline(); 
     $('[dsid="footer"]').text('Browser mode'); 
 } 

}

// On device ready
function onDeviceReady() {
checkConnection();
listServiceExecute();
}

// On offline
function onOffline() {
localStorage.setItem('_isOnline', 0);
$('[dsid="footer"]').text('Offline');
}

// On online
function onOnline() {
localStorage.setItem('_isOnline', 1);
$('[dsid="footer"]').text('Online');
startSynchronization();
}

// List service execute
function listServiceExecute() {
var isOnline = localStorage.getItem('isOnline');
if (isOnline == 1) {
list_service.service.requestOptions.echo = '';
list_service.execute({});
} else {
list_service.service.requestOptions.echo = localStorage.getItem('echo') ? localStorage.getItem('_echo') : '';
list_service.execute();
}
}

// Delete service execute
function deleteServiceExecute() {
var isOnline = localStorage.getItem('isOnline');
if (isOnline == 1) {
delete_service.execute({});
} else {
var idToDelete = localStorage.getItem('taskId');
var nameToDelete = localStorage.getItem('taskName');
if (idToDelete) {
addTaskToDelete(idToDelete, nameToDelete);
} else {
var tasks = localStorage.getItem('tasksToCreate');
var tasksObj = eval('(['+ tasks + '])');
removeTaskFromCreateList(nameToDelete, tasksObj);
tasks = JSON.stringify(tasksObj);
localStorage.setItem('_tasksToCreate', tasks);
}

Code: Select all

     var tmpEcho = localStorage.getItem('_echo'); 
     tmpEcho = eval('('+ tmpEcho + ')'); 
     tmpEcho = removeTaskFromLocalList(idToDelete, tmpEcho, nameToDelete); 
     tmpEcho = JSON.stringify(tmpEcho); 
     localStorage.setItem('_echo', tmpEcho); 

     listServiceExecute(); 
 } 

}

// Create service execute
function createServiceExecute() {
var isOnline = localStorage.getItem('isOnline');
var nameToCreate = localStorage.getItem('taskName');
if (nameToCreate && nameToCreate.trim()) {
if (isOnline == 1) {
create_service.execute({});
} else {

Code: Select all

         addTaskToCreate(nameToCreate); 
         var tmpEcho = localStorage.getItem('_echo'); 
         tmpEcho = eval('(' + tmpEcho + ')') || []; 
         tmpEcho = addTaskToLocalList(nameToCreate, tmpEcho); 
         tmpEcho = JSON.stringify(tmpEcho); 
         localStorage.setItem('_echo', tmpEcho); 

         listServiceExecute(); 
     } 
 } 

}


Changes of the todo-main js to make it my own for offline functionality

Posted: Wed May 21, 2014 10:18 pm
by Yurii Orishchuk

Hi Dave.

Unfortunately your question is not clear enough.

Please provide more details:

What do you want to implement?

What have you tried?

What is not working?

etc.


Changes of the todo-main js to make it my own for offline functionality

Posted: Wed May 21, 2014 10:26 pm
by Dave Troxel

Hi Yurii, I am still trying to get an offline sqlite db to launch and provide data to my application. I have gotten to the point I can see my tables in the Dev Console of the browser, now need it to identify it is offline, and create the db locally on a mobile device.

When testing on my mobile device, I can see the db list when online, but when I back out and exit the app, turn off the wireless connection, and restart the app, I click on the button to take me to my list and I see the little wheel turn for a split second, then nothing.


Changes of the todo-main js to make it my own for offline functionality

Posted: Thu May 22, 2014 8:57 am
by Maryna Brodina

Hello Dave!

Please let's continue within one post https://getsatisfaction.com/apperyio/...