Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Creating a subview by making a subquery in the datacollection

$
0
0

Hi,

I have created a main component and a sub component. Both are filled using the same Collection (SQLite database). The main component lists all the database entries with "parentId=0" (toplevel data entries). The entries are then displayed. My goal is to add a subview, that is opened when an element in the main view is clicked. The subview should fetch data from the table, dependent on the main item that was clicked.

In short there is a table with id, parentid & title:

id  parentid    title
1   0           'Test1'
2   1           'Test2'
3   0           'Test3'
4   3           'Test4'
5   3           'Test5'
6   3           'Test6'
Then the main view is presented by selecting all entries with parentid=0 from the collection, and placing them in tablerows. An onClick function is also set up which handles the event where the table is clicked. The row that is clicked is then retrieved.
// On first start, only the entries with parentid = 0 are displayed on the screen using three labels, and the dataCollection is coupled to the labels using:
if ($model) {
    $.companyItem.model = $model.toJSON();
}
The result on screen is then:
1   0   Test1
3   0   Test3
In the mainView.js this looks like the following:
// mainView.js
var ivrEntries = Alloy.Collections.ivrEntries;
ivrEntries.fetch({query:'SELECT * FROM ' + ivrEntries.config.adapter.collection_name + ' WHERE parentId="' + '0' + '"'});
 
function openId(e) {  
    if (e.row.model) {
        alert(e.row.model); // Works, the data belonging to the row is displayed
        var detailObj=ivrEntries.get(e.row.model);
 
        var win = Alloy.createController('subview',{"$model":detailObj}).getView();  
        $.navController.openWindow(win);
    }
}
In the subview I then do this:
var args = arguments[0] || {};
 
if (args.$model) {
 
    var sub = Alloy.Collections.ivrEntries;
    sub.fetch({query:'SELECT * FROM ' + sub.config.adapter.collection_name + ' WHERE parentId="' + args.$model.id + '"'});  
    var dataJson = args.$model.toJSON();
 
} else {
    alert('data not passed');
}
With this I hope to accomplish that the subview receives the data belonging to the clicked mainview, then fetches the relevant database entries, and displays them in a new window. When in the mainview the label "Test3" is clicked, a new window should appear with the entries having parentid=3, so:
4   3   'Test4'
5   3   'Test5'
6   3   'Test6'
What I see is that when the "sub.fetch({..." is performed, the Labels in the mainview are updated with the new data. The "Alloy.Collections.ivrEntries" collections seems hard-coupled with the mainview, but I want to couple it to the new window that is opened, and show those entries there.

What point am I missing?


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>