钛合金控制器构造函数不公开函数 [英] Titanium Alloy Controller Constructor doesn't exposer functions
问题描述
我正在从另一个控制器动态创建由小部件制成的行.虽然我似乎可以访问视图,但我无法访问导出的函数.
I am creating a row made from a widget dynamically from another controller. While I seem to have access to the views I cannot access the exported functions.
这是小部件的控制器代码.
Here is the controller code for the widget.
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()"函数时.它找不到它.
When I call the "sendMessage()" function from another controller. It can't find it.
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 文件中生成小部件,则此问题不存在.
Anyone knows what I need to do to access those functions? It seems that if the widget is generated in the view XML file this issue doesn't exist.
推荐答案
假设这是您的小部件 .xml:
Lets say that this is your widget .xml:
<Alloy>
<View id="widget">
<Label id="userNameLabel"/>
<Label id="userRatingLabel"/>
<Label id="bodyLabel"/>
<Label id="timeLabel"/>
<Label id="statusLabel"/>
</View>
</Alloy>
在您的 .js 控制器中,您可以将功能添加到您的小部件,或直接设置它,例如:
In your .js controller you could add the function to your widget, or set it directly, like:
$.widget.sendMessage = sendMessage;
称之为:
var widget = Alloy.createWidget("chatDetail.chatRow",{}).getView();
widget.sendMessage();
win.add(widget);
或者到根目录,如果你还没有调用'getView()':
Or to the root, if you didn't call 'getView()' yet:
$.sendMessage = sendMessage;
称之为:
var widget = Alloy.createWidget("chatDetail.chatRow",{});
widget.sendMessage();
win.add(widget.getView());
这篇关于钛合金控制器构造函数不公开函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!