[英]Looking for the best way to export and import a variable between modules
因此,我正在使用Appcelerator学习CommmonJS,并试图找出在ui模块中使用一个模块(即数组)中的变量的最佳方法。 我的问题是,在我的ui模块中,我有ui的主窗口(全局),并且在按钮上有一个eventlistener,它调用一个函数(在同一模块中)以打开地图。 该映射需要从上一个模块中获取的变量。 只是不确定什么是最好的方法。 任何建议都很棒。
数据.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();
好吧,我想出了什么问题,尽管就像大多数事情一样,它只是确定了另一个问题,哈哈。 在这种情况下,它可以将整个过程变成一个函数,并将.openWindow方法作为其自己的函数移至事件侦听器。
buttonView.addEventListener("click", function(){navWin.openWindow(secWin);});
我认为您应该选择其他方式。
在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
模块就不会知道在哪里以及如何使用它(应该如此)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.