Created
December 20, 2011 12:43
-
-
Save hal-gh/1501450 to your computer and use it in GitHub Desktop.
CommonJS Stateful Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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