Is there a step by step for using Services with a Ti app?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
philosurfer's picture

I have seen two presentations on Titanium now at 2 different camps. Only one of which was successfully using services with their native app (@BADcamp 2011)

Is there any step by step on getting this going?

if not.. can we start a documentation Wiki?

Comments

Hi, here is some code from

manuelBS's picture

Hi, here is some code from one of our projects. It shows how we load a list (data from drupal views using services with json).
Just contact me for any further questions.

/***************************************************************************************************
*                                                                                                 *
*                              .                                   .                              *                    
*  MMMMM8       M~         M7             +MMMM          M          . . 8M                        * 
*  MM  MM.  ..8..    8: .. M7.O    M=     MM       IO    M    . . . MM    .   7Z.     .8.   .O+   * 
*  MMMMM   MMD .M  MM. MM  MM  MM. M~ .   8MMM . MM. MM  M  MM  MM .MM . M  MM..MM. MM.~MO MM     * 
*  MM  DM  MM   M  MI  7M  M7  MM  M=        NMN.MZ.  M  M  MM  MM  MM   M  M    M  M   MM  MMM.. * 
*  MM  MM. MM   M  MM. MM  M7  MM  MD     ..  MM MM  MM  M  MM  MM  MM.  M  MM. MM  M   MM .  MM  * 
*  MMMMO.  MM   M   :M$7M. M7  MM. ~MM    MMMM.   7MMM   M   MMM.M.  MM  M   MMMZ   M   MM MMMM   * 
*                   M MMM                                                                         *
*                                                                                                 *
***************************************************************************************************/ 

var win = Titanium.UI.currentWindow;

var isAndroid = false;
if (Titanium.Platform.name == 'android') {
  isAndroid = true;
}


// BACKGROUND IMGAE VIEW
var bgImgView = Titanium.UI.createImageView({url:'../logo.png', width: 320, height:202, left: 0, bottom:0});
win.add(bgImgView);


/
* VIEWS DECLARATION
*/
var _table = Titanium.UI.createTableView({
  top:0, left:0, bottom:0, right:0,
  backgroundColor:'transparent'
});
Titanium.UI.currentWindow.add(_table);

var _actInd = Titanium.UI.createActivityIndicator({
      style:Titanium.UI.iPhone.ActivityIndicatorStyle.BIG,
      height:120,
      width:120,
     
});
if (isAndroid) _actInd.message = 'News are loaded.';
Titanium.UI.currentWindow.add(_actInd);
_actInd.show();






/

  * FUNCTIONS
  */
function addTableWithContent(titles, texts, nids)
{
  var content = [];
  for (var i=0; i < titles.length; ++i)
  {
      var row = Titanium.UI.createTableViewRow({
        backgroundColor:'transparent',
        height:80,

        data:nids[i]
      });

      var title = Titanium.UI.createLabel({
            text:titles[i],
            font:{fontSize:17,fontWeight:'bold'}, color:'#000000', textAlign:'left', backgroundColor:'transparent',

            top:0, left:10, right:10, height:80
      });
    
      row.add(title);
      content.push(row);
     
  }
  _table.data = content;
   _actInd.hide();
  Titanium.UI.currentWindow.remove(_actInd);

}

function getContentForTeamTable()
{
  if (!Titanium.Network.online)
  {
    if (Titanium.App.Properties.getList('rss_title') == null || Titanium.App.Properties.getList('rss_nid') == null)
    {
      alert('Online data not available!');
      return;
    }
    var titleArray = Titanium.App.Properties.getList('rss_title');
    //var textArray = Titanium.App.Properties.getList('rss_text');
    var nidArray = Titanium.App.Properties.getList('rss_nid');
    addTableWithContent(titleArray, textArray, nidArray);
    return;
  }
 
  var nidArray = [];
  var titleArray = [];
  var textArray = [];
 
  var method = 'views.get';
  var viewName = 'News';
  var xhr = Titanium.Network.createHTTPClient();
  xhr.onload = function()
  {
    Ti.API.info(this.responseText);
    var jsonData = eval('(' + this.responseText + ')');
    for (var i=0; i < jsonData['#data'].length; ++i)
    {
      titleArray.push(jsonData["#data"][i].node_title);
      //var strTagStrippedText = jsonData["#data"][i].node_revisions_body.replace(/<\/?[^>]+(>|$)/g, "");
     // textArray.push(strTagStrippedText);
      nidArray.push(jsonData["#data"][i].nid);
    }
    addTableWithContent(titleArray, textArray, nidArray);
    Titanium.App.Properties.setList('rss_title',titleArray);
    //Titanium.App.Properties.setList('rss_text',textArray);
    Titanium.App.Properties.setList('rss_nid',nidArray);
  };
  xhr.onerror = function()
  {
    var tArray = Titanium.App.Properties.getList('teamlist_title');
    addTableWithContent(tArray, pArray);
  };
  xhr.open('POST','http://URL_TO_DRUPAL/services/json');
  xhr.send({'method':method, 'view_name':viewName});
}





/
* LOAD CONTENT & DISPLAY
*/
getContentForTeamTable();


/

* ACTIONS
*/
_table.addEventListener('click', function(e)
{
  var win = null;
  win = Titanium.UI.createWindow({
        title:"",
        url:'detail.js',
        barColor:'#000',
        backgroundColor:'#FBF9F2',
        nid:e.rowData.data //nidArray[e.index]
  });
  Titanium.UI.currentTab.open(win,{animated:true});
});

Awesome...

philosurfer's picture

Thank you... I'll take a swipe at lunch tomorrow.


          "we are the cult of personality."

Take a look at

lelizondo's picture

Take a look at http://drupanium.org

Luis

The page is no longer up:

SocialNicheGuru's picture

The page is no longer up: http://drupanium.org