一种将(添加隐私)封装到 MVC 模型的方法? [英] A way to encapsulte ( add privacy ) to models in the MVC?

查看:20
本文介绍了一种将(添加隐私)封装到 MVC 模型的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我命名我的模型和一个控制器,如下所示:

I namespace my models and a single controller like this:

var MC = {};

然后根据需要添加属性.

and then added properties as needed.

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

For example, the code that initializes all my models looks like this:

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() 存在,则执行它.

I plan on changing this to a loop...just loop through MC, and if init() exists execute it.

这个架构唯一让我烦恼的是,没有任何传统意义上的隐私或封装,即一切都是公开的.

The only thing that bothers me about this architecture is there is no privacy or encapsulation in the traditional sense that everything is public.

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

Also, I don't instantiate my models, I just call them as functions, but this is pretty much the same as instantiation in .js.

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

Is there a simple way to add privacy to my models and still have them accessible as properties in an object.

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

Just for an example here is my simplest model :

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 事件或类似事件调用.

pre() runs before an ajax call, post() after, and init() is called via an onload event or similar.

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

Here is the controller that actually implements this.

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 );  
            }

        } );
    }
};

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

I would like to add privacy. How do I do this and still keep my models accessible as object properties?

相关

如何从本地范围?

推荐答案

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

Is there a simple way to add privacy to my models and still have them accessible as properties in an object?

显然,您需要将整个对象设为私有.是的,这也适用于 JavaScript 中的任何其他隐私 - 闭包.

Obviously, you then will need to make the whole object private. And yes, that will work as any other privacy in JavaScript, too - a closure.

查看模块模式(网上有很多关于这方面的教程).

Have a look at the module pattern (there are many tutorials out on the web about this).

这篇关于一种将(添加隐私)封装到 MVC 模型的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆