3

我命名我的模型和一个这样的控制器:

var MC = {};

然后根据需要添加属性。

例如,初始化我所有模型的代码如下所示:

MC.initAll = function() {

    MC.MASettings.init();
    MC.MATweet.init();

    MC.MUserTry.init();
    MC.MUserNew.init();
    MC.MUserExist.init();

    Su.UserOut.init();
    Su.Media.init();
}

我计划将其更改为循环...只需循环通过 MC,如果 init() 存在则执行它。

这种架构唯一困扰我的是没有传统意义上的隐私或封装,即一切都是公开的。

另外,我不实例化我的模型,我只是将它们称为函数,但这与 .js 中的实例化几乎相同。

有没有一种简单的方法可以为我的模型添加隐私,并且仍然可以将它们作为对象中的属性进行访问。

仅举一个例子,这里是我最简单的模型:

MC.MUserTry = {
    init: function() {
        document.getElementById( 'ut_but' ).addEventListener( "click", function( ) {
            MC.Controller( MC.o_p( 'MUserTry' ) );
        }, false );
    },
    pre : function( o_p ) {
        o_p.page.email = document.getElementById( 'ut_but' ).getAttribute( 'data-email' );
        return o_p; 
    },
    post : function( o_p ) {
        sLocal( o_p.server.hash, o_p.server.privacy, o_p.server.name, o_p.server.picture, 'ma' );
        vStateUpdate( o_p.server.name, o_p.server.picture, o_p.server.privacy );
        vTPane( o_p.server.tweets ); vBPane( o_p.server.bookmarks ); 
        vFlipP( 'ma' ); 
    }
};

pre() 在 ajax 调用之前运行,post() 之后运行,init() 通过 onload 事件或类似事件调用。

这是实际实现这一点的控制器。

MC.Controller = function( o_p ) {

   console.log( 'o_p = ' + o_p.model );

   var t1, t2, t3, t4, 
       i1, i2, i3, i4,
       o_p_string_send;

    if( SU.get('debug') ) {
        t1 = new Date().getTime();
    }

    o_p = MC[ o_p.model ].pre( o_p );  

    if ( o_p.result !== 'complete' ) {
        o_p_string_send = JSON.stringify( o_p );

        if( SU.get('debug') ) {
            t2 = new Date().getTime();
            console.log( '---------------Server Send: \n ' + o_p_string_send );
        } 

        cMachine( 'pipe=' + o_p_string_send , function( o_p_string_receive ) {

            if( SU.get('debug') ) { 
                console.log( '---------------Server Receive: \n ' + o_p_string_receive ); 
                t3 = new Date().getTime();
            }

            o_p.server = JSON.parse( o_p_string_receive );

            MC[ o_p.model ].post( o_p );

            if( SU.get('debug') ) {
                t4 = new Date().getTime(); i1 = t2-t1 ; i2 = t3-t2 ; i3 = t4-t3; i4 = o_p.server.time;
                console.log( '---------------Time: \n Pre | Transit | Post | Server = ', i1, ' | ', i2, ' | ', i3,' | ', i4 );  
            }

        } );
    }
};

我想添加隐私。我该如何做到这一点并且仍然保持我的模型作为对象属性可访问?

有关的

如何从本地范围创建全局的、基于实例的对象?

4

2 回答 2

2

你可以用闭包来做到这一点。

var MyNamespace = {};
MyNamespace.MyClass = (function() {
    function privateMethod() {
        // ...
    }

    function publicMethod() {
        // can call privateMethod() from in here
    }

    return ({
        publicMethod: publicMethod
    });
}());

这将创建一个MyNamespace.MyClass使用一种私有方法和一种公共方法调用的对象。在您的情况下,您可能希望为每个模型类执行此操作。让他们的init方法公开并隐藏他们的其他方法。

于 2012-08-03T23:28:52.740 回答
1

有没有一种简单的方法可以为我的模型添加隐私,并且仍然可以将它们作为对象中的属性进行访问?

显然,您需要将整个对象设为私有。是的,这也可以像 JavaScript 中的任何其他隐私一样工作——闭包。

看看模块模式(网上有很多关于这个的教程)。

于 2012-08-03T23:20:33.243 回答