Hi, what I want to do works fine with iPhone, but not with Android. I have to say that I'm not an JavaScript expert, may be I do some basic thing wrong. This is my code snippet of file DatasetListController.js:
function DatasetListController(aDatasetType, aMainTab) { var DatasetListView = require('ui/DatasetListView'); this.datasetListView = new DatasetListView(aDatasetType); this.listWindow = this.datasetListView.listWindow; Ti.API.info('listWindow '+ this.listWindow.tile); // hide searchbar, because the searchbar has the focus and shows the keyboard on Android if (Ti.Platform.osname == 'android') { this.listWindow.addEventListener('open', function(e) { setTimeout(function(e) { // ERROR datasetListView.searchBar.blur(); // this.datasetListView doesn't work too }, 250); }); }; .... }This is a snippet of DatasetListView.js
function DatasetListView(aDatasetType) { // create window for datasetlist this.listWindow = Ti.UI.createWindow({ title: aDatasetType, backgroundColor: '#fff', tabBarHidden: true }); this.searchBar = Titanium.UI.createSearchBar({ barColor:'#000', showCancel:true, height:43, top:0, }); // create main tableview this.listView = Ti.UI.createTableView({ //style: Ti.UI.iPhone.TableViewStyle.GROUPED, scrollable: true, search: this.searchBar }); this.listWindow.add(this.listView); return this; }; module.exports = DatasetListView;I always get an error when accessing datasetListView in my 'open'-event of my listWindow. On iPhone it works fine. I only have problems with Android.
3 Answers
Accepted Answer
You've encountered a glitch in the iOS implementation of CommonJS. On iOS, you can access the global namespace from within a CommonJS module.
CommonJS modules should not have access to globals; the Android behavior is correct. Your best bet is to avoid global variables entirely. You can always define a CommonJS module that can maintain a map of global variables in a static variable. Anybody who needs to access those values can just require your module to get to them.
It's also not possible to access variables from an internal function in the same js-File:
function DatasetListController(aDatasetType, aMainTab) { var DatasetListView = require('ui/DatasetListView'); this.datasetListView = new DatasetListView(aDatasetType); ... getData(); // call internal function return this; }; // INTERNAL FUNCTIONS function getData() { // ERROR datasetListView is undefined Ti.API.info('listWindow2 '+ datasetListView.listWindow.title); // this.datasetListView doesn't work, too };What I'm doing wrong?
hi,
you can try this code
function DatasetListController(aDatasetType, aMainTab) { var DatasetListView = require('ui/DatasetListView'); this.listWindow = new DatasetListView(aDatasetType); this.datasetListView = this.listWindow.listView; Ti.API.info('listWindow '+ this.listWindow.tile); // hide searchbar, because the searchbar has the focus and shows the keyboard on Android if (Ti.Platform.osname == 'android') { this.listWindow.addEventListener('open', function(e) { setTimeout(function(e) { // ERROR datasetListView.searchBar.blur(); // this.datasetListView doesn't work too }, 250); }); }; .... } function DatasetListView(aDatasetType) { // create window for datasetlist var listWindow = Ti.UI.createWindow({ title: aDatasetType, backgroundColor: '#fff', tabBarHidden: true }); var searchBar = Titanium.UI.createSearchBar({ barColor:'#000', showCancel:true, height:43, top:0, }); // create main tableview var listView = Ti.UI.createTableView({ //style: Ti.UI.iPhone.TableViewStyle.GROUPED, scrollable: true, search: searchBar }); listWindow.listView = listView; listWindow.add(listView); return listWindow; }; module.exports = DatasetListView;
Your Answer
Think you can help? Login to answer this question!