Wahhab_Mirza
Wahhab_Mirza

Reputation: 63

Accessing a function from other .js file

Hi I want to access a function from other .js file.Actually there are two .js files.In one js file i have write the function as.This is db.js

function quizfun() {
   var quizes = db.execute('select * from Quiz');
   while (quiz.isValidRow()) {
       var counter = 0;
       dataArray[counter] = quiz.fieldByName('Quiz_Text');
       quiz.next();
       alert(dataArray[counter]);
       counter++;
   };
   return dataArray;
}

and i am accessing it from other js file which is quizwin.js like this but it is not accessing function

var quiz_db = Titanium.include('db.js');
 quiz_db.quizfun();

Upvotes: 1

Views: 532

Answers (1)

Dawson Toth
Dawson Toth

Reputation: 5670

What you want is to use CommonJS modules. They let you encapsulate logic, and explicitly define an API for other files to use.

Here's your example, but with CommonJS:

db.js:

/* Public API: */
exports.quizfun = quizfun;

/* Implementation: */
function quizfun() {
    // put your quiz fun logic here
}

quizwin.js:

var db = require('db'); // notice no ".js" extension
var dataArray = db.quizfun();

You can read more about CommonJS here: http://developer.appcelerator.com/blog/2011/12/commonjs-module-guide-for-1-8.html

Upvotes: 3

Related Questions