2016-05-30 52 views
0

我從另一個控制器動態創建一個由小部件構成的行。雖然我似乎有權訪問視圖,但無法訪問導出的功能。鈦合金控制器構造函數不暴露函數

這是控件的控制器代碼。

var moment = require("/lib/moment-with-locales"); 
var args = $.args; 
var isSent = true; 

function configure(data){ 
    $.userImage.image = data.otherUser.attributes["avatar-thumb-url"]; 
    $.userNameLabel.text = Alloy.Globals.getFormattedName(data.otherUser.attributes["first-name"], data.otherUser.attributes["last-name"]); 
    //$.userRatingLabel.text = data.userRating; 
    $.bodyLabel.text = data.messages[0].attributes.body; 
    $.timeLabel.text = new moment(data.messages[0].attributes.timestamp).fromNow(); 
} 

function setSentStatus(sent){ 
    isSent = sent; 
    $.statusLabel.height = 15; 
    if(sent == false){ 
     $.statusLabel.color = Alloy.CFG.colors.red; 
    } else { 
     $.statusLabel.color = Alloy.CFG.colors.grey0; 
    } 
}; 

function sendMessage(){ 
    Alloy.Globals.fluidAPI.postMessage(JSON.stringify({ 
     "data": { 
     "type": "message", 
     "attributes": { 
      "body": data.messages[0].attributes.body, 
      "recipient_id": data.otherUser.id 
     } 
     } 
    }), function(postMessageResponse){ 
     if(postMessageResponse){ 
      Ti.API.info(postMessageResponse); 
     } 
    }); 
} 

exports.configure = configure; 
exports.setSentStatus = setSentStatus; 
exports.sendMessage = sendMessage; 

configure(args.data); 

當我從另一個控制器調用「sendMessage()」函數。它找不到它。

var row = Alloy.createWidget("chatDetail.chatRow", {message: {attributes:{body: $.toolbarTextArea.getValue(), timestamp: new moment().toISOString()}}, user: Alloy.Globals.currentUserData}); 
     Ti.API.info(row); 
     controllers.push(row); 
     $.tableView.appendRow(row.getView()); 
     $.tableView.scrollToIndex($.tableView.data[0].rows.length-1); 
     row.sendMessage(); 

任何人都知道我需要做什麼來訪問這些功能?看起來,如果在視圖XML文件中生成小部件,則此問題不存在。

+0

當你'Ti.API.info(行);'這是什麼打印?我不是說它是你期望的模塊的一個實例。 – developer82

+0

您可以將全局fns文件放入'lib'文件夾中,並將此文件包含在您的控制器文件中。 –

+0

它打印控制器的所有參數,但由於某些原因缺少功能。 –

回答

2

比方說,這是你的小部件的.xml:

<Alloy> 
    <View id="widget"> 
     <Label id="userNameLabel"/> 
     <Label id="userRatingLabel"/> 
     <Label id="bodyLabel"/> 
     <Label id="timeLabel"/> 
     <Label id="statusLabel"/> 
    </View> 
</Alloy> 

在你的.js控制器則可以將功能添加到您的小工具,或者直接設置它,如:

$.widget.sendMessage = sendMessage; 

呼叫它:

var widget = Alloy.createWidget("chatDetail.chatRow",{}).getView(); 
widget.sendMessage(); 
win.add(widget); 

或根,如果你不叫 'getView()' 尚未:

$.sendMessage = sendMessage; 

叫它:

var widget = Alloy.createWidget("chatDetail.chatRow",{}); 
widget.sendMessage(); 
win.add(widget.getView()); 
相關問題