Titanium Alloy Controller Constructor doesn't exposer functions

妖精的绣舞 提交于 2019-12-25 07:17:11

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/37533973/titanium-alloy-controller-constructor-doesnt-exposer-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!