Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CommonJs+;钛_Javascript_Titanium_Commonjs - Fatal编程技术网

Javascript CommonJs+;钛

Javascript CommonJs+;钛,javascript,titanium,commonjs,Javascript,Titanium,Commonjs,在我看来,这个问题比钛更接近于普通。 我编写了一个大文件。相当丑陋(第一次代码和平)。你想跳就跳 问题是:我的代码中有2个视图,我想把它们放到不同的文件中 不幸的是,我在export.modules方面做得并不好。检查代码的第二个和第三个和平 var fenetreBase = Titanium.UI.createWindow({fullscreen:true,backgroundColor:"white",exitOnClose:true}); fenetreBase.open();

在我看来,这个问题比钛更接近于普通。 我编写了一个大文件。相当丑陋(第一次代码和平)。你想跳就跳

问题是:我的代码中有2个视图,我想把它们放到不同的文件中 不幸的是,我在export.modules方面做得并不好。检查代码的第二个和第三个和平

 var fenetreBase =   Titanium.UI.createWindow({fullscreen:true,backgroundColor:"white",exitOnClose:true});
fenetreBase.open();

var viewimage = Titanium.UI.createView({backgroundColor:'red'});
var logo = Titanium.UI.createImageView({ image:'/image/test.jpg', top:0, left:0, width:"10%", height:"7%"});
var label1 = Ti.UI.createLabel({ backgroundColor:"blue", color: "white", text: 'Graphe', textAlign:Titanium.UI.TEXT_ALIGNMENT_CENTER, left:"10%", width: "90%", height:"7%", top:0});
var logo2 = Titanium.UI.createImageView({ image:'/image/test.jpg', top:0, left:0, width:"10%", height:"7%"});
var label2 = Ti.UI.createLabel({ backgroundColor:"blue", color: "white", text: 'Graphe', textAlign:Titanium.UI.TEXT_ALIGNMENT_CENTER, left:"10%", width: "90%", height:"7%", top:0});
var mapvisu = Titanium.UI.createImageView({ image:'/image/test.jpg', top:"7%",left:0, width:"100%",height:"93%"});
var viewgraphe = Titanium.UI.createView({backgroundColor:'red'});
var concentration = Titanium.UI.createImageView({image:'/image/test.jpg',top:"7%",left:0,width:"100%",height:"31%"});
var meteo = Titanium.UI.createImageView({image:'/image/test.jpg',top:"38%",left:0,width:"100%",height:"31%"});
var emission = Titanium.UI.createImageView({image:'/image/test.jpg',top:"69%",left:0,width:"100%",height:"31%"});

viewgraphe.add(label2);
viewgraphe.add(logo2);
viewgraphe.add(concentration);
viewgraphe.add(meteo);
viewgraphe.add(emission);

viewimage.add(logo);
viewimage.add(label1);
viewimage.add(mapvisu);

fenetreBase.add(viewimage);
fenetreBase.add(viewgraphe);

viewimage.visible = false;
viewgraphe.visible = true;
我想要3个文件:“app.js”、“vuemage.js”、“vuegraph.js”=

var fenetreBase =  Titanium.UI.createWindow({fullscreen:true,backgroundColor:"white",exitOnClose:true});
fenetreBase.open();

vueImage = require("vueimage");
vueGraphe = require("VueGraphe");

fenetreBase.add(vueImage ou vueGraphe)//depends need.
使用vuemage.js和vuegraph.js时,如下所示:

function vueimage(title) { var self = Ti.UI.createView({backgroundColor:'white'});
self.add(.....);//item i need
};

module.exports = vueimage;

如果有人能告诉我怎么弄明白那件事。我所有的努力都以惨淡的失败或艰难的关闭而告终s、

首先,请务必阅读Tappcelerator关于在钛合金中使用commonJS的指南:

作为一个快速的背景,commonJS是一个javascript库,用于在javascript中加载依赖项,这使得将javascript程序分解为独立的代码片段变得很容易,每个代码都在自己的范围内运行

Titanium使用SDK版本1.8中的commonJS。并且使用它可以为您提供非常干净的代码,可以分解为定义的片段,在它们自己的范围内运行,并且对性能非常好

在Tianium中,您可以通过以下方式之一使用它:(1)创建一个具有函数的模块,您可以从需要它们的对象调用该函数(这与静态方法非常相似),或者(2)让整个模块作为一个具有自己的函数(原型)的对象,并由一个函数公开

例如:

模块(myModule.js):

应用程序(App.js):

另一种方法(我想这就是你想要的):

模块(myView.js):

应用程序(App.js):

当然,还有其他方法可以实现这一点,比如创建您需要的东西,并实现createMyView函数(参见peracetic继承)


希望这能回答您的问题。

它有效!我可能忘了回来。也谢谢你的文档。
function sayHello(name) {
      alert('Hello ' + name);
}
exports.sayHello = sayHello;
var myModule = require('/myModule');
myModule.sayHello('developer82');
function myView() {
     var some_view = Ti.UI.createView({ /* properties */ });
     // your logic comes here
     return some_view;
}

module.exports = myView;
var myView = new (require('/myView'))();