【问题标题】:Durandal : view and viewModel still persists in heap memoryDurandal:视图和视图模型仍然存在于堆内存中
【发布时间】:2014-11-02 19:37:13
【问题描述】:

我正在与 durandalknockout 合作开展大型 SPA 项目。

我们正面临内存泄漏问题。

通常我们在一个页面中,我们需要转到下一页。

当我们转到下一页时,浏览器堆被清除,反之亦然。

在我们的 SPA 中路由时,我们需要清除当前页面 hap 大小(内存)。

durandal compose 绑定有什么办法吗?。

【问题讨论】:

  • 您的 ViewModel 看起来如何,激活和停用功能?如果你的 require 函数返回一个 ctor durandal 应该每次都创建一个新实例
  • 当你改变视图时你的视图模型会重新实例化吗?
  • 停用已被提及。导航到另一个页面时,您可以在那里释放任何消耗内存的东西

标签: knockout.js durandal single-page-application


【解决方案1】:
<!-- HTML Page Templates -->

    <article class="main-user-page">
        <div data-bind="compose: {model: '/viewmodel/viewLoginUser',view:'view/viewLoginUser'}">

        </div>
    </article >

    <article class="main-user-list-page">
        <div data-bind="compose: {model: '/viewmodel/user/viewUserList',view:'view/user/viewUserList'}">

        </div>
    </article >

    <article class="user-list-page">
        <div data-bind="forEach: userList">
            <span data-bind="Name"></span>
            <span data-bind="email"></span>
            <span data-bind="Role"></span>
        </div>
    </article >
<code>
<pre>
/*we are using Durandal for routing and composeing template 
  all files have same Structure 
*/
function define(['services/utilities', 'durandal/app', 'durandal/system', 'services/logger', 'config', 'services/datacontext','services/bindingHandler'],
    function (util, app, system, logger, config, datacontext) {
    var userList = ko.observableArray([]);

    var userObj = ko.observable();
    function activate(){
        return datacontext.getUserList().then(result){
            userList(result);
        }
    }
    function deactivate(){
        userList.removeAll();
    }
    return {
        userList: userList,
        userObj: userObj
    }
});

</pre>

</code>

【讨论】:

  • 这对你有用吗?您是否必须始终清除停用中的淘汰赛绑定?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
相关资源
最近更新 更多