Quantcast
Viewing all articles
Browse latest Browse all 8068

organize module, commonJS passing parameter

i want to move the function getDis to another module, how to do that?

app.js i create a layout

var gallery = require('gallery');
 
var tableRow=[];
var pageNum =1;
 
var win = Ti.UI.createWindow({
    title:'test Manage Data'
});
 
win.addEventListener('open',function(e){
    gallery.funcTableView(DisTableView);    
});
 
var btnNext = Ti.UI.createButton({
    title:'Next'
});
 
var table = Ti.UI.createTableView({
    data:tableRow
});
 
function DisTableView(jsonString){ //display data
        var rowBtn = Ti.UI.createTableViewRow();
 
    for(var i=0;i<jsonString.files.length;i++){
        var row = Ti.UI.createTableViewRow();
 
        var img = Ti.UI.createImageView({
            image:jsonString.files[i].thumb,
            right:10,
            height:50,
            width:50    
        });
 
        var label = Ti.UI.createLabel({
            text:jsonString.files[i].filename,
            left:10
        });
 
        //when first time load need id?
        row.ID=jsonString.files[i].id;
        row.add(img);
        row.add(label);
        tableRow.push(row);
    }
    table.setData(tableRow);
    rowBtn.add(btnNext);
    table.appendRow(rowBtn);
};
btnNext.addEventListener('click',function(e){
    pageNum++;
    gallery.funcTableView(DisTableView);
});
table.addEventListener('click',function(e){
    if(e.row.ID){
        var win2 = require('win2');
        var win2open = win2.win2(e.row.ID);
        win2open.open();
    }
});
win.add(table);
win.open();
gallery.js i create the http access to server get the data
exports.funcTableView=getDataView;
 
function getDataView(call){
    var client = Ti.Network.createHTTPClient({
        onload:function(e){
 
            var jsonString = JSON.parse(this.responseText);
            call(jsonString);
        },
        onerror:function(e){
        },
        timeout:20000
    });
    client.open('GET','http://gallery.mobile9.com/c/ringtones_4157/1/?view=json&pg='+ pageNum);
    client.send();
};
 
exports.funcClick = getRow;
function getRow(callback,ID){
 
    var client = Ti.Network.createHTTPClient({
 
        onload:function(e){
            var jsonString=JSON.parse(this.responseText);
            callback(jsonString);
        },
        onerror:function(e){
            alert('Connection Lost');
        },
        timeout:20000
    });
    client.open('GET','http://gallery.mobile9.com/f/'+ ID +'/?view=json');
    client.send();
};
on gallery.js i play a music ( but i want move the getDis function to new module, how to do that?)
var gallery1 = require('gallery');
 
exports.win2 = function(ID){
 
var win = Ti.UI.createWindow({
    title:'Play Media'
});
 
win.addEventListener('open',function(e){
 
    gallery1.funcClick(getDis,ID);
 
    var btnPlay = Ti.UI.createButton({
        title:'Play',
        bottom:10,
        right:10    
    });
 
    var btnPause = Ti.UI.createButton({
        title:'Pause',
        left:10,
        bottom:10
    });
 
    var btnStop = Ti.UI.createButton({
        title:'Stop',
        bottom:10,
        right:120
    });
 
    function getDis(jsonString){
 
    var progressBar = Ti.UI.createProgressBar({
        bottom:70,
        width:250,
        min:0,
        max:100,
        value:0,
        color:'#fff',
        message:'Playing 0 of 100',
        font:{fontSize:14, fontWeight:'bold'},
    });
 
    var img = Ti.UI.createImageView({
        image:jsonString.thumb,
        width:250,
        height:250,
        top:10,
    });
 
    var audioPlayer = Ti.Media.createAudioPlayer({
        url:jsonString.media_file
    });
 
    audioPlayer.addEventListener('progress',function(e){
        var progressValue = ((Math.round(e.progress))/jsonString.duration)/100;
            progressBar.value = progressValue*10;
            progressBar.message ='Playing '+ progressBar.value.toFixed(2) +' of 100';
            Ti.API.info('value is '+ progressValue);
    });
 
    win.add(img);       
    audioPlayer.start();
    win.add(progressBar);
    progressBar.show();
 
    btnPlay.addEventListener('click',function(){
        audioPlayer.start();
 
    });
 
    btnStop.addEventListener('click',function(){
        progressBar.value = 0;
        audioPlayer.stop();
    });
 
    btnPause.addEventListener('click',function(){
         audioPlayer.pause();
    });
 
    win.add(btnPlay);
    win.add(btnStop);
    win.add(btnPause);
 
};
    win.activity.actionBar.setDisplayHomeAsUp(true);
    win.activity.actionBar.onHomeIconItemSelected = function (){ 
    win.close();
    };
 
});
 
return win;
};

Viewing all articles
Browse latest Browse all 8068

Trending Articles