Uploaded image for project: 'Appcelerator Studio'
  1. Appcelerator Studio
  2. TISTUD-1305

Add support for CommonJS modules to JS content assist

    Details

    • Type: Story
    • Status: Closed
    • Priority: High
    • Resolution: Won't Fix
    • Affects Version/s: Titanium Studio 1.0.9
    • Fix Version/s: Backlog
    • Component/s: Content Assist, JS
    • Labels:

      Description

      Below are several simple examples of a CommonJS Module

      module_1.js

      //helper function to construct light views
      function createLightView(color,on) {
      	return Ti.UI.createView({
      		backgroundColor:color,
      		top:10,
      		width:60,
      		height:60,
      		opacity:(on) ? 1 : 0.2
      	});
      }
       
      //main component constructor
      function StopLightView() {
      	//create component instance
      	var self = Ti.UI.createView({
      		height:220,
      		width:80,
      		backgroundColor:'#232323',
      		layout:'vertical'
      	});
       
      	//create state/instance data
      	var lightColor = 'red';
       
      	//construct UI
      	var greenLight = createLightView('green');
      	self.add(greenLight);
       
      	var yellowLight = createLightView('yellow');
      	self.add(yellowLight);
       
      	var redLight = createLightView('red', true);
      	self.add(redLight);
       
      	//private helper function to handle changing of light colors
      	function changeColor(newColor) {
      		//update object state
      		lightColor = newColor;
       
      		//toggle opacity, based on the color requested
      		switch(newColor) {
      			case 'green':
      				greenLight.opacity = 1;
      				yellowLight.opacity = 0.2;
      				redLight.opacity = 0.2;
      				break;
      			case 'yellow':
      				greenLight.opacity = 0.2;
      				yellowLight.opacity = 1;
      				redLight.opacity = 0.2;
      				break;
      			default:
      				greenLight.opacity = 0.2;
      				yellowLight.opacity = 0.2;
      				redLight.opacity = 1;
      				break;
      		}
       
      		//when light change is complete, fire custom event on component instance
      		self.fireEvent('lightchange', {
      			color:lightColor
      		});
      	}
       
      	//create public component interface
       
      	//create an accessor for the current color state
      	//CAUTION - "getXXX" and "setXXX" functions are RESERVED on iOS, and you can't name functions with that prefix!
      	self.lightColor = function() {
      		return lightColor;
      	};
       
      	//after a transitional period, change the color from yellow to red
      	self.stop = function() {
      		if (lightColor !== 'red') {
      			changeColor('yellow');
      			setTimeout(function() {
      				changeColor('red');
      			},1500);
      		}
      	};
       
      	//change the light color to green for a time, then stop
      	self.go = function() {
      		changeColor('green');
      		setTimeout(function() {
      			self.stop();
      		},4000);
      	};
       
      	//return component instance
      	return self;
      }
       
      //make component constructor the public interface for the module
      module.exports = StopLightView;
      

      module_2.js

      /**
       * description
       * @classDescription This class creates a new Test.
       * @return {Test} Returns a new Test.
       * @type {Object} A class with constructor and prototype methods
       */
      var Test = function() {
          var _question = [];
       
          /**
           * Adds a question to the internal stack
           * @return {Number} The index of the added question
           */
          this.addQuestion = function(question) {
              return _question.push(question); //TODO: possible 1-based need to take measure if cross-platform
          };
       
          /**
           * getQuestion
           * @return {Object} A Question object
           */
          this.getQuestion = function(index) {
          	return _question[index];
          };
          /**
           * getAllQuestion
           * @return {Array} A reference to the internal question array
           */
          this.getAllQuestion = function() {
          	return _question;
          }
      };
       
      /**
       * Blah
       * @return {void}
       */
      Test.blah = function(){
      	return void(0);
      }
       
      /**
       * Takes a function 
       * @return {Array}
       */
      Test.prototype.eachPossibility = function(questionIndex, fn) {
      	//Array.prototype.forEach.call(this.getAllQuestion()[questionIndex].possibilities, fn)
      	return this.getAllQuestion()[questionIndex].possibilities.forEach(fn);
      }
       
       
      // fix cross debug output and add support for multiple arguments to Ti debug
      this.console || (this.console = {}, console.log = function(){ Ti.API.debug( Array.prototype.slice.call(arguments) ); });
      // Check if we're in a CommonJS environment
      if( typeof require == 'function' && typeof module == 'object' ) {
          exports['quiz'] = Test;
      }
      

      module_3.js

      (function (Ti, Titanium, exports) {
          var Test;
          if (Titanium == undefined /* Android */) {
              Test = {};
              exports.bootstrap = function (TiSDK) {
                  Ti = Titanium = TiSDK;
                  return Test;
              };
          }
          else {
              Test = exports;
          }
          
          Test.retrieveSuccessLabel = function () {
              return Ti.UI.createLabel({
                  text: 'Success!', textAlign: 'center',
                  color: '#000'
              });
          };
      })(Ti, Titanium, exports);
      

      module_4.js

      exports.info = function(str) {
          Titanium.API.info(new Date()+': '+str);
      };
       
      exports.debug = function(str) {
          Titanium.API.debug(new Date()+': '+str);
      };
      

      module_5.js

      module.exports = {
         someprop: 'coolbeans'
      };
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                cwilliams Christopher Williams
                Reporter:
                klindsey Kevin Lindsey
              • Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: