简体   繁体   English

管理CommonJS模块时出现问题

[英]Problems managing CommonJS modules

I am using Titanium Appcelerator to develop apps using JavaScript. 我正在使用Titanium Appcelerator来使用JavaScript开发应用程序。 They have suggested to use the CommonJS approach. 他们建议使用CommonJS方法。 A brief example on CommonJS can be found here . 此处可以找到有关CommonJS的简短示例。

For the life of me, I cannot still figure out how to structure my code. 对于我的一生,我仍然无法弄清楚如何构造代码。

Example: 例:

/* Homescreen.js */
exports.createHomescreen = function () {

    //load all required modules first
    var videoPlayer = require('ui/videoPlayerModule');

    var self = Ti.UI.createWindow({
        width:'100%',
        height:'100%'
    })

    var newPlayer = videoPlayer.createPlayer({
        width:100
        height:50
    });

    self.add(newPlayer);
    return self;
}

The videoPlayerModule videoPlayerModule

/* videoPlayerModule.js */
exports.createPlayer = function (object) {

    //load all required modules first
    var self = Ti.UI.createWindow({
        width:object.width,
        height:object.height
    });

    var exitVideoButton = Ti.UI.createButton({
        width:100,
        height:50
    });

    exitVideoButton.addEventListener('click',function(e){
        self.close();    //When this window is closed, the memory isn't freed.
        self = null;     //Still the memory isn't cleared
    });

    self.add(exitVideoButton);

    return(self);
}

I am having memory allocation problems because whenever I load a videoPlayer and close it, the memory is never cleared. 我遇到了内存分配问题,因为每当我加载videoPlayer并将其关闭时,就永远不会清除内存。 If I open the videoPlayer again, the memory is allocated AGAIN. 如果我再次打开videoPlayer,则会再次分配内存。 Because of this, my app's memory usage increases every time the videoPlayer is launched. 因此,每次启动videoPlayer时,我的应用程序的内存使用量都会增加。

I know my way of thinking is not right. 我知道我的思维方式不正确。 I am overlooking something very simple here. 我在这里忽略了一些非常简单的事情。 Can anyone let me know what am I not doing right? 谁能让我知道我在做什么不对?

This is happening because you are adding a Ti.UI.Window (created from videoPlayerModule.js), to another Ti.UI.Window (in Homescreen.js), which you should not be doing. 发生这种情况的原因是,您正在将Ti.UI.Window (从videoPlayerModule.js创建)添加到另一个Ti.UI.Window (在Homescreen.js中),您不应该这样做。 Ti.UI.Window is a base container object, you never add it to anything (generally), so when you close the window, it still stays referenced as one of the children of the container window, so it never goes away. Ti.UI.Window是基础容器对象,通常不会将其添加到任何内容中,因此,当关闭窗口时,它仍被引用为容器窗口的子级之一,因此它永远不会消失。 Your self = null; 你的self = null; does nothing at this point. 在这一点上什么也不做。

You need to replace your Window in video player with a View, I would try something like this instead: 您需要将视频播放器中的Window替换为View,我会尝试这样的操作:

/* videoPlayerModule.js */
exports.createPlayer = function (object) {

    var self = Ti.UI.createView({
        width:object.width,
        height:object.height
    });

    var exitVideoButton = Ti.UI.createButton({
        width:100,
        height:50
    });

    exitVideoButton.addEventListener('click',function(e){
        self.hide();
    });

    self.add(exitVideoButton);

    return(self);
}

This is not a complete solution, as you still will have the view in memory, but it is a much much smaller footprint then a full fledged window, a better way to do this would be to create this once, then show() or hide() it when needed in the context of the Homescreen, another way would be to pass the parent and then remove the view from its parent when exited, but this solves your memory issue. 这不是一个完整的解决方案,因为您仍将视图保留在内存中,但它的占用空间要比完整的窗口小得多,一种更好的方法是创建一次,然后执行show()hide()在主屏幕的上下文中需要时,另一种方法是传递父级,然后在退出时从其父级中删除视图,但这解决了您的内存问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM