如何从小部件中获取变量?钛

时间:2014-06-28 17:52:23

标签: javascript widget titanium

控制器代码段:

Widget.xml

function buildDataInGrid(scrollView,images){
    activity.show();
    for (var imageInfo in images) {
        var frameView = Ti.UI.createView({
            item : images[imageInfo],
            width : columnWidth,
            height : columnHeight,
            backgroundImage: WPATH('images/itemShadow.png'),
            top : 0,
            left : (space / 2),
            right : space,
            bottom : space
        });

        //Can be local image or from the Web
        var imageLocation = images[imageInfo].location;
        var imageView = Ti.UI.createImageView({
            touchEnabled : false,
            image:imageLocation,
            borderColor:"#363636",
            borderWidth:2,
            top: 0,
            left : 0,
            width : columnWidth - (columnWidth * 0.05),
            height : columnHeight - (columnHeight * 0.06)
        });

        frameView.add(imageView);

        if (options.showTitle) {
            var osname = Ti.Platform.osname;
            version = Ti.Platform.version, height = Ti.Platform.displayCaps.platformHeight, width = Ti.Platform.displayCaps.platformWidth;
            if (osname === 'iphone')
                options.titleFontSize = 11;

            var titleView = Ti.UI.createLabel({
                touchEnabled : false,
                text : images[imageInfo].title,
                textAlign : 'center',
                color : 'white',
                width : columnWidth - (columnWidth * 0.051), //offset 5% of from background shadow
                height : '15%',
                opacity : '0.85',
                left : 0,
                bottom : "6%",
                backgroundColor : '#363636',
                font : {
                    fontSize : options.titleFontSize,
                    fontWeight: "bold"
                }
            });
            frameView.add(titleView);
        }
        scrollView.add(frameView);

        //Wire up Touch Events
        //Single Tap
        frameView.addEventListener('singletap', function(e) {
            alert("You've selected : " + e.source.item.title);
            alert("You've selected : " + e.source.item.id);

        });
        //Double Tap
        frameView.addEventListener('doubletap', function(e) {
            alert("You've selected : " + e.source.item.title);

    }
    activity.hide();
}

photogallery.xml

<Alloy>
    <Window id="win" backgroundColor="#504d4d" >
        <RightNavButton >
            <Button id="btnRightChat" title='Chat' onClick="rightButtonClicked"/>
        </RightNavButton>
        <ScrollView id="scrollGeneric" >
            <View id="winContainer">
                 <Require type="widget" src="com.imagitechdj.imageGrid" id="imageGrid"/>
            </View>
            <View id = "view_indicator" width= '100' height= '100' borderRadius='10'>
                <ActivityIndicator id="ind"  />
            </View>
            <View id = "viewNoInt"> 
                <View id='LabelIntConnectionView'>
                <Label id="lblNoInt"/>
                </View>
            </View> 
        </ScrollView>
    </Window>
</Alloy>

我想在按下click事件后将widget.sub中的变量e.source.item.title传递给photo gallery.js,我该如何传递它?

我使用过全局事件处理程序,但会导致内存泄漏。

根据评论更新:

Widget.js

exports.addEventListener=frameView.addEventListener;

/************* Pricate Functions ************************/
function buildDataInGrid(scrollView,images){
    activity.show();
    for (var imageInfo in images) {
        var frameView = Ti.UI.createView({
            item : images[imageInfo],
            width : columnWidth,
            height : columnHeight,
            backgroundImage: WPATH('images/itemShadow.png'),
            top : 0,
            left : (space / 2),
            right : space,
            bottom : space
        });

        //Can be local image or from the Web
        var imageLocation = images[imageInfo].location;
        var imageView = Ti.UI.createImageView({
            touchEnabled : false,
            image:imageLocation,
            borderColor:"#363636",
            borderWidth:2,
            top: 0,
            left : 0,
            width : columnWidth - (columnWidth * 0.05),
            height : columnHeight - (columnHeight * 0.06)
        });

        frameView.add(imageView);

        if (options.showTitle) {
            var osname = Ti.Platform.osname;
            version = Ti.Platform.version, height = Ti.Platform.displayCaps.platformHeight, width = Ti.Platform.displayCaps.platformWidth;
            if (osname === 'iphone')
                options.titleFontSize = 11;

            var titleView = Ti.UI.createLabel({
                touchEnabled : false,
                text : images[imageInfo].title,
                textAlign : 'center',
                color : 'white',
                width : columnWidth - (columnWidth * 0.051), //offset 5% of from background shadow
                height : '15%',
                opacity : '0.85',
                left : 0,
                bottom : "6%",
                backgroundColor : '#363636',
                font : {
                    fontSize : options.titleFontSize,
                    fontWeight: "bold"
                }
            });
            frameView.add(titleView);
        }
        scrollView.add(frameView);

        //Wire up Touch Events
        //Single Tap
        frameView.addEventListener('singletap', function(e) {

//do something


        });
        //Double Tap
        frameView.addEventListener('doubletap', function(e) {

        });
    }
    activity.hide();
}

photogallery.js

$.imageGrid.addEventListener('singletap', function(e) {
    alert("You've selected : " + e.source.item.title);
    alert("You've selected : " + e.source.item.id);
});


[ERROR] :      line = 161;
[ERROR] :      message = "Can't find variable: frameView";
[ERROR] :      name = ReferenceError;
[ERROR] :      sourceId = 318245792;

我想这可能是因为它无法找到事件监听器。

1 个答案:

答案 0 :(得分:0)

我编辑过帖子: 您必须从窗口小部件导出事件侦听器:

//add this  in buildDataInGrid function
exports.addEventListener=scrollView.addEventListener;
exports.on = function(name, cb) { return scrollView.addEventListener(name, cb); };
exports.off = function(name, cb) { return scrollView.removeEventListener(name, cb); };
exports._hasListenersForEventType = function(name, flag) {
        return scrollView._hasListenersForEventType(name, flag);
      };

然后在照相馆:

yourwidget.addEventListener('singletap', function(e) {
            alert("You've selected : " + e.source.item.title);
            alert("You've selected : " + e.source.item.id);
    });