我正在从另一个控制器动态创建一个小部件。虽然我似乎可以访问视图但我无法访问导出的函数。
这是小部件的控制器代码。
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 :(得分: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);
或者到root,如果你还没有调用'getView()':
$.sendMessage = sendMessage;
称之为:
var widget = Alloy.createWidget("chatDetail.chatRow",{});
widget.sendMessage();
win.add(widget.getView());