钛合金控制器构造器没有暴露函数[英] Titanium Alloy Controller Constructor doesn't exposer functions

本文是小编为大家收集整理的关于钛合金控制器构造器没有暴露函数的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

以下是窗口小部件的控制器代码.

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();

任何人都知道我需要做些什么来访问这些功能?似乎如果在View XML文件中生成窗口小部件,则此问题不存在.

推荐答案

让我们说这是你的小部件.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());

本文地址:https://www.itbaoku.cn/post/1938212.html

问题描述

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);

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();

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.

推荐答案

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>

In your .js controller you could add the function to your widget, or set it directly, like:

$.widget.sendMessage = sendMessage;

Call it:

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

Or to the root, if you didn't call 'getView()' yet:

$.sendMessage = sendMessage;

Call it:

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