所以我正在使用Appcelerator学习CommmonJS,我试图找到在我的ui模块中使用来自一个模块(这是一个数组)的变量的最佳方法。我的问题是,在我的ui模块中,我有ui的主窗口(全局),并且我在按钮上有一个eventlistener,它调用一个打开地图的函数(在同一个模块中)。此映射需要我从前一个模块获取的变量。只是不确定最好的方法是什么。任何建议都很棒。
Data.js
var read = function(){
console.log("---read is activated---");
var db = Ti.Database.open('fourSqDB');
var rows = db.execute('SELECT fourSqID, name, lat, lng, distance, type FROM fourSqTBL');
var dbArray = [];
while (rows.isValidRow()){
var record = {
fourSqID: rows.fieldByName('fourSqID'),
name: rows.fieldByName('name'),
lat: rows.fieldByName('lat'),
lng: rows.fieldByName('lng'),
distance: rows.fieldByName('distance'),
type: rows.fieldByName('type'),
};
dbArray.push(record);
rows.next();
}
rows.close();
db.close();
var getUI = require('ui'); //go into ui.js
//getUI.buildUI(dbArray); //fire buildMainUI function
getUI.buildSecUI(dbArray); //fire buildSecUI function
return dbArray; }; //close read function
ui.js
var win = Ti.UI.createWindow({
layout: "vertical",
backgroundColor: "#ccc"
//background: "images/bg.png"
});
//Navigation Window
var navWin = Ti.UI.iOS.createNavigationWindow({
window: win,
});
var label1 = Ti.UI.createLabel({
//top: 250,
textAlign: "center",
text: "This is the shisha app created by Emmanuel Farrar for AVF1512 Week 3 assignment "
});
var labelButton = Ti.UI.createLabel({
text: "Go To Data",
color: "white"
});
var statementView = Ti.UI.createView({
borderColor: "black", borderRadius: 10,
top: 10, bottom: 0,
height: 300, width: 350
});
var buttonView = Ti.UI.createView({
backgroundColor: "#1f2f34",
top: statementView.bottom + 200,
borderRadius: 10,
height: 75,
width: 350
});
///////////////////////////////////////////
// buildSecUI function (map and listing)
///////////////////////////////////////////
var buildSecUI = function(dbArray){ //dbArray from data.js read function
console.log("---buildSecUI is activated---");
console.log(dbArray);
var secWin = Ti.UI.createWindow({
layout: "vertical",
backgroundColor: "#ffffff"
});
//building the map
var Map = require('ti.map');
var mapView = Map.createView({
mapType: Map.NORMAL_TYPE,
region: {latitude:25.2867, longitude:51.5333,
latitudeDelta:0.05, longitudeDelta:0.05},
animate:true,
regionFit:true,
userLocation:true
});
//array for annotations (pins for maps)
var annotations = [];
for ( var i = 0 ; i < dbArray.length; i++ ) {
// this section will create annotations and add them on the mapView
var pin = Map.createAnnotation({
latitude: dbArray[i].lat,
longitude: dbArray[i].lng,
title: dbArray[i].name,
type: dbArray[i].type,
animate:true,
pincolor:Map.ANNOTATION_PURPLE
});
annotations[i] = pin;
//annotations.push(pin);
// console.log(annotations[i]);
console.log("Pin Info: " + + "Lat" + pin.latitude + " / " + "Lng "+ pin.longtitude);
mapView.addAnnotation(annotations[i]); // adds annotations
} //for loop closure
secWin.add(mapView);
navWin.openWindow(secWin);
}; //closure for buildSecUI
exports.buildSecUI = buildSecUI;
//event listner
buttonView.addEventListener("click", buildSecUI); //closure for buildSecUI
// adding stuff here
statementView.add(label1);
buttonView.add(labelButton);
win.add(statementView, buttonView);
navWin.open();
答案 0 :(得分:0)
好吧,我想到了问题是什么,但大多数事情只是发现了另一个问题,哈哈。在这种情况下,它可以将整个事物转换为函数,并将.openWindow方法作为自己的函数移动到事件侦听器。
buttonView.addEventListener("click", function(){navWin.openWindow(secWin);});
答案 1 :(得分:0)
我认为你应该走另一条路。
在data.js
导出read
功能,但省略两条getUI
行。
exports.read = function() {
// your code except the two getUI lines
return dbArray;
};
然后在ui.js
中需要data
模块,调用导出的read()
并使用它构建UI。
var data = require('data');
var dbArray = data.read();
buildSecUI(dbArray);
这样你data
模块就不知道它的使用位置和方式(应该是这样)。