【问题标题】:Titanium Alloy Controller Constructor doesn't exposer functions钛合金控制器构造函数不暴露功能
【发布时间】:2016-05-30 21:46:04
【问题描述】:

我正在从另一个控制器动态创建由小部件组成的行。虽然我似乎可以访问视图,但我无法访问导出的函数。

这是小部件的控制器代码。

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 文件中生成的,则似乎不存在此问题。

【问题讨论】:

  • 当您执行Ti.API.info(row); 时,它会打印什么?我不认为它是您期望的模块的一个实例。
  • 您可以将全局 fns 放入 lib 文件夹中的文件中,并将此文件包含在您的控制器文件中
  • 它打印控制器的所有参数,但由于某种原因缺少功能。

标签: titanium appcelerator appcelerator-titanium


【解决方案1】:

假设这是您的小部件 .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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多