Search code examples
formsappceleratortitanium-mobiletitanium-alloy

Titanium Classic or Alloy for Dynamic Forms


I want to develop an application which is getting form data via JSON from an external database. I need to create the form fields and its properties according this dynamic data.

It seemed to me that I need to use the classical way in Titanium instead of alloy because I think I can not add any rows dynamically on the xml (view) side in Alloy. Am I correct or is it also possible to do it in Alloy? If yes can you please tell me how


Solution

  • This can be done. Using this widget https://github.com/albinotonnina/it.numidia.gridWidget I was able to figure out how to create dynamic content in Alloy. Similar to the method used in this widget, I have a controller for each item I want to support. I created a textfield, textarea, label among others. It allows me to still use the Alloy styling and dynamically add the elements to my views.

    Here is an example of my textfield controller:

    XML

    <Alloy>
        <TextField id="textfield"/>
    </Alloy> 
    

    js

    function applyProperties(_props){
        var apply = {};
    
        _.extend(apply, _.pick(_props, 'left', 'value', 'textAlign', 'font', 'color', 'shadowOff'));
        // alert(apply);
        $.textfield.applyProperties(apply);
    }
    
    exports.getContent = function(){
        return $.textfield.value;
    };
    exports.setContent = function(val){
        $.textfield.value = val;
    };
    
    
    if(arguments[0]){
        applyProperties(arguments[0]);
    }
    
    exports.applyProperties = applyProperties;
    

    The style is completely empty since I'm using the app.tss to style this element.