Skip to content

Instantly share code, notes, and snippets.

@hal-gh
Created December 20, 2011 12:43
Show Gist options
  • Save hal-gh/1501450 to your computer and use it in GitHub Desktop.
Save hal-gh/1501450 to your computer and use it in GitHub Desktop.
CommonJS Stateful Example
var stateful = require('statefulModule');
var score = require('scoreModule');
var window = Ti.UI.createWindow({
backgroundColor:'white',
fullscreen:false,
title:'Click window to score'
});
window.addEventListener('click', function() {
try {
Ti.API.info("The latest score is " + score.latestScore());
Ti.API.info("Adding " + stateful.getPointStep() + " points to score...");
score.pointsWon();
Ti.API.info("The latest score is " + score.latestScore());
Ti.API.info("Setting points per win to 10");
stateful.setPointStep(10);
Ti.API.info("Adding " + stateful.getPointStep() + " points to score...");
score.pointsWon();
Ti.API.info("The latest score is " + score.latestScore());
Ti.API.info("---------- Info ----------");
Ti.API.info("stateful.getPointStep() returns: " + stateful.getPointStep());
Ti.API.info("stateful.stepVal value is: " + stateful.stepVal); // will always return default of 5
Ti.API.info("**************************");
} catch(e) {
alert("An error has occurred: " + e);
}
});
window.open();
var appStateful = require('statefulModule'); // a reference to the "stateful" variable in app.js that contains the stateful module
var _score = 0; // default
exports.pointsWon = function() {
_score += appStateful.getPointStep();
};
exports.pointsLost = function() {
_score -= appStateful.getPointStep();
};
exports.latestScore = function() {
return _score;
};
var _stepVal = 5; // default
exports.setPointStep = function(value) {
_stepVal = value;
};
exports.getPointStep = function() {
return _stepVal;
};
exports.stepVal = _stepVal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment