【问题标题】:Looking for the best way to export and import a variable between modules寻找在模块之间导出和导入变量的最佳方式
【发布时间】:2015-12-28 19:02:29
【问题描述】:

所以我正在使用 Appcelerator 学习 CommmonJS,并且我试图找出在我的 ui 模块中使用来自一个模块(它是一个数组)的变量的最佳方法。我的问题是,在我的 ui 模块中,我有 ui 的主窗口(全局),并且我在一个按钮上有一个事件监听器,它调用一个打开地图的函数(在同一模块中)。此地图需要我从上一个模块中获得的变量。只是不确定最好的方法是什么。任何建议都会很棒。

数据.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();

【问题讨论】:

    标签: javascript export appcelerator commonjs


    【解决方案1】:

    我认为你应该走另一条路。

    data.js 中导出read 函数,但省略两个getUI 行。

    exports.read = function() {
      // your code except the two getUI lines
      return dbArray;
    };
    

    然后在ui.js require data 模块中,调用导出的read() 并用它构建UI。

    var data = require('data');
    var dbArray = data.read();
    buildSecUI(dbArray);
    

    这样您data 模块就不会知道它在哪里以及如何使用(应该如此)。

    【讨论】:

    • 谢谢,我会试试这个。也许这会解决我的新问题
    【解决方案2】:

    好吧,我知道问题出在哪里,尽管就像大多数事情一样,它只是发现了另一个问题,哈哈。在这种情况下,它将整个事情变成一个函数,并将 .openWindow 方法作为它自己的函数移动到事件侦听器。

    buttonView.addEventListener("click", function(){navWin.openWindow(secWin);}); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      相关资源
      最近更新 更多