Monday, December 12, 2016

Working with commonJS module pattern.

app.js


var win = Ti.UI.createWindow({
backgroundColor : 'green',
layout : "vertical"

});

win.open();

// Create a Button.
var request = Ti.UI.createButton({
title : 'Add view',
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
top : 20,

});

request.addEventListener('click', function(e) {

var addView = require('/lib/viewFile');

var label_view = new addView();

win.add(label_view);
});

win.add(request);


// Create a Button.
var nextWindow = Ti.UI.createButton({
title : 'nextWindow',
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
top : 50

});

nextWindow.addEventListener('click', function(e) {


var nextPage = require('lib/newWin');
var Next_win = new nextPage();
Next_win.open();
//nextPage.createData();


});

win.add(nextWindow);



//ExampleView File

lib/viewFile.js


function view() {

var view = Ti.UI.createView({
width : Ti.UI.SIZE,
height : Ti.UI.SIZE,
layout : "vertical",
backgroundColor : 'red'
});

var mars = Ti.UI.createLabel({
text : 'Top 10 pics from Mars!',
top : 20,
width : Ti.UI.SIZE,
height : 30

});
view.add(mars);

view.addEventListener("click", function(e) {

var hello = require("/lib/operation");
hello.sayHello("Mars is bigger than earth");

});

// Creating label in window B, note that the same transitionName is used.
var marsB = Ti.UI.createLabel({
text : 'Top 10 pics from Mars!',

top : 20,
width : Ti.UI.SIZE,
height : 30,

});

view.add(marsB);

return view;
};
module.exports view;


/lib/operation.js


var defaultMessage = "Hello world";
// we make objects, variables, functions available to the
// calling context by adding them to the exports object
exports.sayHello = function(msg) {
alert('Hello '+msg);
};
// we can assign other objects, functions, and variables to
// exports and they will be available to the calling context
exports.helloWorld = function() {
Ti.API.info(defaultMessage);
};


/lib/newWin.js


function newWin() {

var window = Ti.UI.createWindow({
backgroundColor : 'white'
});

var textField = Ti.UI.createTextField({
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color : '#336699',
width : 250,
height : 60
});

textField.addEventListener('click', function() {
Ti.API.info('in text field click');
});

window.add(textField);

return window;

}


module.exports = newWin;



//Project Structure










No comments:

Post a Comment