简体   繁体   English

CommonJs +钛

[英]CommonJs + titanium

this question is closer from commonJs than titanium in my mind. 在我看来,这个问题比普通人更接近钛。 i code a big file. 我编写一个大文件。 quite ugly (1st peace of code). 非常难看(代码的第一个和平)。 You can jump it if you want. 如果你愿意,你可以跳它。

The probleme : I have 2 view in my code ,i would like to put them into diferent file unfortunatly, i do nothing well with : export.modules. 问题:我在我的代码中有2个视图,我想不幸地将它们放入不同的文件中,我不做任何事情:export.modules。 check the seconde & third peace of code 检查代码和第三代码的和平

 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;

i 'd like 3 file : "app.js" , "vueimage.js" , "vueGraphe.js".with app.js = 我想要3个文件:“app.js”,“vueimage.js”,“vueGraphe.js”.with app.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.

with vueimage.js & vuegraphe.js look like that : 与vueimage.js和vuegraphe.js看起来像这样:

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

module.exports = vueimage;

if sb can tell me how to figure out that thing. 如果某人可以告诉我如何找出那件事。 All my try were concluded by dismal failure or hard shutdown. 我所有的尝试都是通过惨淡失败或硬关机来完成的。 :s. :秒。

Well, first of all make sure you read this guide by Tappcelerator about using commonJS in Titanium: https://wiki.appcelerator.org/display/guides/CommonJS+Modules+in+Titanium 好吧,首先请确保您阅读Tappcelerator关于在Titanium中使用commonJS的指南: https ://wiki.appcelerator.org/display/guides/CommonJS+Modules+in+Titanium

A bit of a quick background, commonJS is a javascript library for dependency loading in javascript, which makes it easy to break a javascript program into pieces of independent code, each running in it's own scope. 有点快速的背景,commonJS是一个用于javascript中依赖加载的javascript库,这使得将javascript程序分解为多个独立代码很容易,每个代码都运行在它自己的范围内。

Titanium makes use of commonJS from version 1.8 of the SDK. Titanium使用SDK的1.8版本的commonJS。 and using it gives you great clean code, breakable into defined pieces, running in their own scoped, and very great for performance. 并使用它为您提供了非常干净的代码,可以分解成定义的部分,在自己的范围内运行,并且非常适合性能。

in Titanium you could use this in one of the following ways: (1) you either make a module with function that you can call from the object requiring them (which is pretty much like static methods), or (2) you have the entire module act as an object with it's own functions (prototyped) and exposed by one function. 在Titanium中,您可以通过以下方式之一使用它:(1)您要么创建一个具有可以从需要它们的对象调用的函数的模块(这与静态方法非常相似),或者(2)您拥有整个模块模块充当具有其自身功能(原型)的对象,并由一个函数公开。

for example: 例如:

Module (myModule.js): 模块(myModule.js):

function sayHello(name) {
      alert('Hello ' + name);
}
exports.sayHello = sayHello;

App (app.js): 应用程序(app.js):

var myModule = require('/myModule');
myModule.sayHello('developer82');

the other way to do that (and this one I think is what you're looking for): 另一种方法(我认为这是你正在寻找的):

Module (myView.js): 模块(myView.js):

function myView() {
     var some_view = Ti.UI.createView({ /* properties */ });
     // your logic comes here
     return some_view;
}

module.exports = myView;

App (app.js): 应用程序(app.js):

var myView = new (require('/myView'))();

of course there are other ways to go about this like creating something that you can require and implement something like createMyView function (see peracetic inheritance). 当然还有其他方法可以解决这个问题,比如创建一些你需要的东西,并实现类似createMyView函数的东西(参见peracetic inheritance)。

hope this answers you question. 希望这能回答你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM