简体   繁体   English

实例化后,javascript订阅回调函数

[英]javascript subscribe to callback function after instanciation

I have a Javascript Object made this way: 我有这样一个Javascript对象:

function App(callback){

// Call some "long running functions meant for initialization
callback(this);

}

In my main Template page (_Layout.cshtml on ASP MVC) I instanciate it like this: 在我的主模板页面(ASP MVC上的_Layout.cshtml)中,我像这样进行实例化:

_Layout.cshtml _Layout.cshtml

var oApp = new App(function(initializedApp){

    // Instanciate here other Javascript components that depend on 
    // App initialization
    var oComponent1 = new Component1(initializedApp);
});

This works fine ! 这很好! Component1 is initialized afte App has finished initializeing and all goes fine. App初始化完成后,Component1已初始化,一切正常。

Proble rises in Page1.cshtml (which is rendered inside _Layout.cshtml and have access to oApp). Page1.cshtml(在_Layout.cshtml内呈现并可以访问oApp)中的问题增加。 Page1 also has to initialize its own component that need App to be fully initialized. Page1还必须初始化其自己的组件,该组件需要完全初始化App。

Page1.cshtml Page1.cshtml

// How do I "subscribe to the App callback function so that Compnent2, s
// specific to the Page1.cstml, is initialized afetr App has finished
// its initialization ???
var oComponent2 = new Compnent2(oApp);

oApp.callback(); // ???
oApp(function(initializedApp){ // code here... });  //???

Thanks 谢谢

Lorenzo 洛伦佐

Your running with an interesting pattern. 您以有趣的模式跑步。 I would suggest using a more unobtrusive way of initializing and calling javascript. 我建议使用一种更加简洁的方法来初始化和调用javascript。 I would say most JS calls in CSHTML can be considered code smell. 我想说CSHTML中的大多数JS调用都可以视为代码气味。

Anyway, I will answer your question. 无论如何,我会回答你的问题。 I would change App to have the ability to register callbacks: 我将更改App以注册回调:

function App(callbacks)
{
    var self = this;
    self.callbacks = [];
    self.addCallbacks = function(newCallbacks)
    {
        //allow either a single callback or an array of callbacks
        if(isFunction(callbacks)
        {
            self.callbacks.push(newCallbacks); //single callback
        }
        else
        {
            for(var i = 0; i < newCallbacks.length; i++)
            {
                //theoretically, you can have multi-dimensional arrays of callbacks, but that's just excessive
                self.addCallbacks(newCallbacks[i]);
            }
        }
    };
    self.addCallbacks(callbacks);

    //some point later, call all the callbacks:
    for(var i = 0; i < self.callbacks.length; i++)
    {
        self.callbacks[i](this);
    }
}

Then, in your Page1.cshtml : 然后,在您的Page1.cshtml

var oComponent2 = new Compnent2(oApp);
var mycallback = function(initializedApp){ // code here... };
oApp.addCallbacks(mycallback);

//or, if you have many callbacks to register:
var mycallbacks = [
    function(initializedApp){ // code here... },
    function(initializedApp){ // code here... }
];
oApp.addCallbacks(mycallbacks);

isFunction borrowed from here : isFunction这里借来的:

var isFunction = function(functionToCheck) 
{
    var getType = {};
    return functionToCheck && getType.toString.call(functionToCheck) === '[object   Function]';
}

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

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