首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CommonJs +钛

CommonJs +钛
EN

Stack Overflow用户
提问于 2013-12-03 16:06:57
回答 1查看 1.1K关注 0票数 0

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

问题:我在我的代码中有两个视图,我想把它们不幸地放入不同的文件中,我对: export.modules没有做好任何事情。检查代码的第二和第三和平

代码语言:javascript
复制
 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“,"vueimage.js”,"vueGraphe.js".with app.js=。

代码语言:javascript
复制
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.

对于vueimage.js & vuegraphe.js,如下所示:

代码语言:javascript
复制
function vueimage(title) { var self = Ti.UI.createView({backgroundColor:'white'});
self.add(.....);//item i need
};

module.exports = vueimage;

如果某人能告诉我怎么弄清楚那件事。我所有的尝试都以令人沮丧的失败或艰难的关闭而结束。S :s.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-04 06:19:08

那么,首先要确保您阅读了Tappcelerator关于在钛中使用commonJS:https://wiki.appcelerator.org/display/guides/CommonJS+Modules+in+Titanium的指南。

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

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

在钛中,您可以使用以下方式之一:(1)您可以使用需要它们的对象(这很像静态方法)调用的函数创建一个模块,或者(2)让整个模块充当一个具有自己的函数(原型)并由一个函数公开的对象。

例如:

模块(myModule.js):

代码语言:javascript
复制
function sayHello(name) {
      alert('Hello ' + name);
}
exports.sayHello = sayHello;

App (app.js):

代码语言:javascript
复制
var myModule = require('/myModule');
myModule.sayHello('developer82');

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

模块(myView.js):

代码语言:javascript
复制
function myView() {
     var some_view = Ti.UI.createView({ /* properties */ });
     // your logic comes here
     return some_view;
}

module.exports = myView;

App (app.js):

代码语言:javascript
复制
var myView = new (require('/myView'))();

当然,还有其他方法可以这样做,比如创建您可以要求的东西,并实现类似createMyView函数的东西(请参阅过氧乙酸继承)。

希望这能回答你的问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20355996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档