将窗口对象传递给Backbone / Marionette AMD应用程序

时间:2014-04-10 08:08:59

标签: javascript php backbone.js marionette

我正在开发一个基于Zend的大型php应用程序,该应用程序基于每个页面请求维护其会话,该请求更新基于前端的JavaScript setTimout变量。这不是后端存储的会话var,而是一个前端setTimeout javaSctipt var,它在一定时间后到期并在每个php页面请求上更新。在Zend之上实现图层后,我在Backbone / Marionette顶层访问此变量时遇到问题,以保持会话同步。我已经尝试将它附加到窗口但是一旦Backbone / Marionette点击路由器并获取其REST api调用,我确实丢失了之前对窗口的所有引用。我试图将这个js存储的超时var从我的php调用传递给Backbone / Marionette路由器,但是一旦我点击Marionette路由器,我就失去了之前对窗口对象的所有引用。我的问题是当我从我的php动作中点击路由器时,如何在我的Backbone / Marionette AMD应用程序中访问此SESSIONREMAINDER全局。它看起来我失去了那个窗口对象

globalscripts.js (包含在生活在某个标题模板中的每个php请求中的文件)

我无法更改但需要访问我的顶层

的遗留代码
var SESSIONREMAINDER;
$(document).ready(function(){

    (function () {
    var nativeSetTimeout = window.setTimeout;

    window.bindTimeout = function (listener, interval) {
        function setTimeout(code, delay) {
            var elapsed = 0,
                h;

            h = window.setInterval(function () {
                    elapsed += interval;
                    if (elapsed < delay) {
                        listener(delay - elapsed);
                    } else {
                        window.clearInterval(h);
                    }
                }, interval);
            return nativeSetTimeout(code, delay);
        }

        window.setTimeout = setTimeout;
        setTimeout._native = nativeSetTimeout;
    };
    }());
    window.bindTimeout(function (t) {
        console.log(t + "ms remaining");
        SESSIONREMAINDER = t;
    }, 10000);      

    var sessionTimeout = window.setTimeout("ModalOverlay.open('/two/phase2/echo2.0/countdown.htm', 400, 300, 'Session Expiration')",2400000);
    this 
});

SingupformController.php(击中Marionette路由器的Zend Controller Action)

  

public function editdraftAction(){

    $formId = $this->getRequest()->getParam('form_id');
    $url = 'https://' . $_SERVER['HTTP_HOST'] . '/form/#id/' . $formId;
    $this->_redirect($url);
}

main.js (所有lib的配置和填充程序)这是我需要访问我之前设置的window.sessionTimeout

的地方

配置

  

require.config({       baseURL:&#39; assets / js&#39;,

paths: {
    jquery: '../../components/jquery/jquery',  // amd version
    jqueryui: '../../components/jquery-ui-1.10.0/ui/jquery-ui',  // amd version
    underscore: '../../components/underscore-amd/underscore', // amd version
    underscoreM: 'libs/underscore/underscore-mustache',  // underscore templating supporting mustache style {{ ... }}
    moment: 'libs/moment/moment.min',  // underscore templating supporting mustache style {{ ... }}
    mustache: '../../components//mustache.js-master/mustache',  // mustache templating {{ ... }}
    backbone: '../../components/backbone-amd/backbone', // amd version
    'backbone.wreqr': '../../components/backbone.wreqr/lib/amd/backbone.wreqr', // amd version
    'backbone.eventbinder': '../../components/backbone.eventbinder/lib/amd/backbone.eventbinder', // amd version
    'backbone.babysitter': '../../components/backbone.babysitter/lib/amd/backbone.babysitter', // amd version
    'backbone.validation': '../../components/backbone.validation-master/src/backbone-validation', // non amd version
    'backbone.relational': '../../components/backbone.relational/backbone-relational', // non amd version
    'backbone.session': '../../components/backbone.session/backbone.session', 
    bbmodal: '../../components/backbone.bootstrap.modal/src/backbone.bootstrap-modal',
    modelbinder: '../../components/backbone.modelbinder/Backbone.ModelBinder', // amd version
    backstack: '../../components/backstack-master/backstack',
    marionette: '../../components/marionette/lib/core/amd/backbone.marionette',  // amd version
    //marionette: '../../components/marionette/lib/core/amd/backbone.marionette.v0.9.12',  // amd version
    bootstrap: '../../components/bootstrap/docs/assets/js/bootstrap', 
    //dr: '../../components/dr/dr.jquery.plugin',
    text: '../../components/requirejs-text/text',
    domReady:'../../components/requirejs-domready/domReady',
    colorpicker: '../../components/jquery/plugins/colorpicker/colorpicker',  // amd version
    notifier: '../../components/backbone.notifier/js/backbone.notifier',  // amd version
    'jquery.spinner': '../../components/jquery/plugins/spin/jquery.spinner', 
    'jquery.cookie': '../../components/jquery/plugins/cookie/jquery.cookie', 
    spin: '../../components/jquery/plugins/spin/spin', // amd version
    utils: '../../components/utils/'
},

shim: {     
    bootstrap: ['jquery'],
    jqueryui: ['jquery'],
    colorpicker: ['jquery'],
    spin: {
        deps: ['jquery'],
        exports: 'Spinner'
    },

    'jquery.spinner':{
        deps:['jquery', 'spin'],
        exports: 'jqspinner'
    },

    bbmodal: {
        deps: ['jquery', 'underscore', 'backbone']
    },        

    dr: ['jquery'],

    'backbone.relational' : {
        deps: ['underscore','backbone']
    }
}

});

和startApp(这两个都是同一个主要的一部分,js我刚刚将它分成两部分,因为代码块有奇怪的预览

  

需要([       &#39;应用&#39 ;,       &#39; jquery的&#39 ;,       &#39; jQueryUI的&#39 ;,       &#39; domready中&#39 ;,       &#39; utils的/调试&#39 ;,       &#39;控制器/ signupformcontroller&#39 ;,       &#39;控制器/ formbuilderrouter&#39 ;,       &#39;控制器/ secondapp&#39],       的startApp);

function startApp(
        App, 
        $, 
        jqueryui, 
        domReady,
        DebugUtils,
        signupFormController, 
        formBuilderRouter, 
        secondApp) {
'use strict';

var options = {
    secondApp: secondApp,
    formBuilderRouter: formBuilderRouter,       
    signupFormController: signupFormController
}; 

$(function() {
    DebugUtils.log('Main App.start(options)');
    DebugUtils.log(options);
    App.start(options);    
});

};

我的问题是,当我点击看起来像这样的index.html时,如何访问全局SESSIONREMAINDER

  

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>Signup Form Builder</title>    
    <meta name="description" content="Backbone Marionette Modular RequireJS 2.0 Bootstrap Responsive">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link href="components/jquery-ui-1.10.0/themes/base/jquery.ui.all.css" rel="stylesheet">
    <link href="assets/css/bootstrap.css" rel="stylesheet">
    <link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
    <link href="assets/css/form-main2.css" rel="stylesheet">
    <link href="assets/css/form-designer.css" rel="stylesheet">
    <link href="assets/css/colorpicker.css" rel="stylesheet">
    <link href="assets/css/styles.css" rel="stylesheet"/>
    <link href="assets/css/dr-stylesheet-ff-compressed.css" rel="stylesheet"/>
    <link href="assets/css/main.css" rel="stylesheet">
    <link href="assets/css/form-builder.css" rel="stylesheet">

    <!-- data-main attribute tells require.js to load assets/js/main.js after require.js loads. -->
    <script  data-main="assets/js/main" src="components/requirejs/require.js"></script>

    <!-- only dependency to require since we don't have it AMD enabled and need cross browser commmunication on app initialization -->
    <script type="text/javascript" src="components/legacy.support/json/json2.js"></script>
    <script type="text/javascript" src="components/porthole/porthole.js"></script>

</head>
<body>
    <div id="main"></div>
    <div id="modal"></div>
    <iframe id="surveyframe" name="surveyframe" frameborder="0" src="/builder/proxies/"></iframe>
</body>
</html>

我可以发布更多代码。

提前致谢!

0 个答案:

没有答案