Titanium: How to reload a tab upon focus? - tabs

I have built a favorite-tab in my application that displays entries in a database that are flagged as such. On the backside this works as it should, but I can't figure out how to reload the favorites tab after changes have been made. When I quit and relaunch the app though, the updates appear as they should.
In essence I have the same problem as is presented here: http://developer.appcelerator.com/question/31211/tab---tableview--database-reload
"I have a 'search' and 'search history' tab in my application. Every search executed under this tab gets inserted into my local db.
However the new search is not reflected in the tableview retrieving data from the DB in the 'search history' tab when i browse to it, after searching.
The latest search, however, gets loaded when I exit the app and relaunch it.
I need some help in figuring out how to trigger the tableview reload without exiting the app.
SOLUTION UPDATE:
I feel 'noobish' for asking this question but it was really so simple
I added a 'focus' eventlistener for the window housing the tab to load the data.
win.addEventListener('focus', function() { loadDBdata(); });
This seems reasonable, but what should the loadDBdata function should contain?
I would greatly appreciate any pointers to this.

i would suggest firing an event when the favorites are updated and having an event listenter on the table that holds the favorites list. When it receives the event, it should update the tables contents.
see this question I just answered it should provide a guide for solving your problem also
Problems with refresh function for page, in Appcelerator

the loaddb function should contain something like:
// init database
var db = Ti.Database.install('path/to/database.db','myDB');
// your request
var sql = 'SELECT * FROM myDatabaseTable';
// the result from that request
var myContent = db.execute(sql);
// need to put the result in a valid structure before inserting into your tableview
var data = [];
while (myContent.isValidRow()){
// your tableviewrow
var row = {
title = myContent.field(0);
};
data.push(row);
myContent.next();
};
// need to close stuff (!)
myContent.close();
db.close();
// finally return data
return data;

Related

Export filtered data from table widget Google AppMaker to a Spreadsheet

Does anyone know how we can export filtered data from Google App Maker table widget.
My table from the data source (no filter):
After applying some filter
.
I manage to export everything to Google Spreadsheet (see this SO answer), but I am stuck on exporting only the data after applying some filter. Really appreciate if anyone can share the solution. Thanks!
It seems that you are looking for filters. It should work if you modify script from this answer to something similar to this:
// Server script
function dataExport(filter1, filter2, ...) {
// TODO: Check permissions.
...
var query = app.models.MyModel.newQuery();
// TODO: Apply user/roles specific filters for
// security reasons.
query.filters.Field1._equals = filter1;
query.filters.Field2._greaterThan = filter2;
...
var filteredRecords = query.run();
filteredRecords.forEach(function(record) {
...
});
...
The AMU Library can do this in one command in a button onClick.
The button just needs to be on the same datasource as the table.
Simply:
AMU.export.toSpreadsheet = function(widget, Your_modelName);

Polymerfire - creating data after an event

I'm using the firebase/polymerfire package from bower bower install firebase/polymerfire
how can i create some data in the database after a method has been triggered?
The document tag looks like it will display and update data. I would like to, when a user signs up, create some data for the user to use.
app.signInAnonymously = function() {
this.error = null;
this.$.auth.signInAnonymously();
// add default data for the user template
};
How can i use the default set() or push methods like the normal SDK?
How can i call this on an event from JavaScript?
When trying to bind a path to my document like
<firebase-document
path="/"
data="{{firebaseData}}">
</firebase-document>
{{firebaseData}}
the data won't display, but I have authentication working.
You can actually use the firebase api directly there since firebase-auth is already including it, but if you want to keep the element-based functionality you could do this:
Add an empty firebase-document to your template
<firebase-document id="mydoc"></firebase-document>
Then call its save function in your element
app.signInAnonymously = function() {
this.error = null;
this.$.auth.signInAnonymously();
// add default data for the user template
//set path to null somewhere to avoid overwriting data, I recommend doing it here since save's path update is lazy
this.$.mydoc.path = null;
//set data to the value to set/push
this.$.mydoc.data = {/*your data*/};
//call save, if the second parameter is falsey it'll call push to the first parameter if it's truthy it'll set the data to firstparam/secondparam
this.$.mydoc.save('path/to/users', userid);
};
As for getting data with firebase-document, check that you actually have data in your database and your security rules, if you still can't see your data then it might be related to this issue, do bear in mind that polymerfire is still in a pre-release state

IndexedDB: When to close a connection

I would like to know what the correct place to close a connection to the database is.
Let's say that I have the following piece of code:
function addItem(dbName, versionNumber, storeName, element, callback){
var requestOpenDB = indexedDB.open(dbName, versionNumber); //IDBRequest
requestOpenDB.onsuccess = function(event){
//console.log ("requestOpenDB.onsuccess ");
var db = event.target.result;
var trans = db.transaction(storeName, "readwrite");
var store = trans.objectStore(storeName);
var requestAdd = store.add(element);
requestAdd.onsuccess = function(event) {
callback("Success");
};
requestAdd.onerror = function(event) {
callback("Error");
};
};
requestOpenDB.onerror = function(event) {
console.log ("Error:" + event.srcElement.error.message);/* handle error */
callback("Error");
};
}
addItem basically adds a new element into the database. As per my understanding, when the requestAdd event is triggered that doesn't mean necessarily that the transaction has finished. Therefore I am wondering what the best place to call db.close() is. I was closing the connection inside of requestAdd.onsucess, but if an error happens and requestAdd.onerror is triggered instead, the connection might still be opened. I am thinking about adding trans.oncomplete just under request.onerror and close the db connection here which might be a better option. Any inputs will be more than welcome. Thank you.
You may wish to explicitly close a connection if you anticipate upgrading your database schema. Here's the scenario:
A user opens your site in one tab (tab #1), and leaves it open.
You push an update to your site, which includes code to upgrade the database schema, increasing the version number.
The same user opens a second tab to your site (tab #2) and it attempts to connect to the database.
If the connection is held open by tab #1, the connection/upgrade attempt by tab #2 will be blocked. Tab #1 will see a "versionchange" event (so it could close on demand); if it doesn't close its connection then tab #2 will see a "blocked" event.
If the connection is not held open by tab #1, then tab #2 will be able to connect and upgrade. If tab #1 then tries (based on user action, etc) to open the database (with an explicit version number) it will fail since it will be using an old version number (since it still has the old code).
You generally never need to close a connection. You are not creating memory leaks or anything like that. Leaving the connection open does not result in a material performance hit.
I would suggest not worrying about it.
Also, whether you add trans.oncomplete before or after request.onerror is not important. I understand how it can be confusing, but the order in which you bind the listeners is irrelevant (qualified: from within the same function scope).
You can call db.close() immediately after creating the transaction
var trans = db.transaction(storeName, "readwrite");
db.close();
and it will close the connection only after the transaction has completed.
https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close says
The connection is not actually closed until all transactions created using this connection are complete. No new transactions can be created for this connection once this method is called.
If you want to run multiple versions of your app and both access the same database, you might think it's possible to keep connections open to both. This is not possible. You must close the database on one before opening it on another. But one problem is that there is currently no way to know when the database actually closes.

How to replace chrome.tabs.onSelectionChanged deprecated method?

I need to get data from the tab the user is leaving (switching on other tab or going on other program).
But chrome.tabs seems not providing an event allowing that..
Before there was apparently chrome.tabs.onSelectionChanged (not tested) but it's deprecated.
And other event are giving the data of the new tab not the one the user just left...
I try also jQuery $(window).blur event, but i have to make a call to the chrome.storage of the tab left by the user (i create a storage for each tab named by the tab id) and i did not get the response of storage in time with this event (the result of the storage is used in a if() to know if i have or not to display an confirm box.
Someone could help me ?
Thx !
To detect a tab change, just use chrome.tabs.onActivated. Since you're interested in the previous tab, store the result of the event at the end of each event. For instance, like this:
var storedWindowInfo = {};
chrome.tabs.onActivated.addListener(function(activeInfo) {
var windowLastTabId = storedWindowInfo[activeInfo.windowId];
if (windowLastTabId) {
// Do something with previous tab, e.g. send a message:
chrome.tabs.sendMessage(windowLastTabId);
}
// Update ID of currently active tab in the current window
storedWindowInfo[activeInfo.windowId] = activeInfo.tabId;
});
Note: Only the tabID and windowID are provided to this event. You need to issue chrome.tabs.query to get more information, such as whether the tab still exists, its URL, etc.

How to Use plugin RowEditing to Updating values

I have a Grid panel and RowEditing Plugin. I search how to use events when i click on Update Button.
I can display alert() but i don't know how to retrieve updates datas ?!!
${prefix}grid.on('validateedit',onEdit, this);
function onEdit(e) {
alert('UPDATE');
};
I have just one line on my Gridpanel. I need to retrieve all data to updating it :)!
Thanks :)
In your onEdit function, you receive the edit object (e), and you can access everything you need to from here.
For example, if I want to access the store... I can do so from within the onEdit function like
var store = e.grid.store;