一种方法来encapsulte(添加隐私)在MVC模式? [英] A way to encapsulte ( add privacy ) to models in the MVC?

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

问题描述

我命名空间我的模特和一个控制器是这样的:

  VAR MC = {};

根据需要

和然后加入属性

例如,code,它初始化我所有的车型看起来是这样的:

  MC.initAll =功能(){    MC.MASettings.init();
    MC.MATweet.init();    MC.MUserTry.init();
    MC.MUserNew.init();
    MC.MUserExist.init();    Su.UserOut.init();
    Su.Media.init();
}

我计划在这个不断变化的一环......通过MC只是循环,如果的init()存在执行它。

这是困扰我的这种架构的唯一事情是不存在隐私或封装在传统意义上,一切都是公开的。

另外,我不实例化我的模型,我就打电话给他们的功能,但是这是pretty大致相同的.js实例。

有一个简单的方法添加隐私我的模型,而且还有他们作为一个对象的属性进行访问。

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

  MC.MUserTry = {
    初始化:功能(){
        的document.getElementById('ut_but')阅读进度(点击,函数(){
            MC.Controller(MC.o_p('MUserTry'));
        },FALSE);
    },
    pre:功能(o_p){
        o_p.page.email =的document.getElementById('ut_but').getAttribute('数据的电子邮件');
        返回o_p;
    },
    岗位:功能(o_p){
        sLocal(o_p.server.hash,o_p.server.privacy,o_p.server.name,o_p.server.picture,'马');
        vStateUpdate(o_p.server.name,o_p.server.picture,o_p.server.privacy);
        vTPane(o_p.server.tweets); vBPane(o_p.server.bookmarks);
        vFlipP('马');
    }
};

pre()运行一个AJAX调用前,后()后,和init()通过onload事件或类似的。

被称为

下面是实际实现此控制器。

  MC.Controller =功能(o_p){   的console.log('o_p ='+ o_p.model);   变种T1,T2,T3,T4,
       I1,I2,I3,I4,
       o_p_string_send;    如果(SU.get('调试')){
        。T1 =新的日期()的getTime();
    }    。o_p = MC [o_p.model] pre(o_p);    如果(o_p.result!=='完成'){
        o_p_string_send = JSON.stringify(o_p);        如果(SU.get('调试')){
            。T2 =新的日期()的getTime();
            的console.log('---------------服务器发送:\\ N'+ o_p_string_send);
        }        cMachine('管='+ o_p_string_send,功能(o_p_string_receive){            如果(SU.get('调试')){
                的console.log('---------------服务器接收:\\ N'+ o_p_string_receive);
                。T3 =新的日期()的getTime();
            }            o_p.server = JSON.parse(o_p_string_receive);            MC [o_p.model]。员额(o_p);            如果(SU.get('调试')){
                。T4 =新的日期()的getTime(); I1 = T2-T1; I2 = T3-T2; I3 = T4-T3; I4 = o_p.server.time;
                的console.log('---------------时间:\\ n pre |交通|邮政|服务器=',I1,'|',I2,'|',I3 ,|,6-14);
            }        });
    }
};

我想补充的隐私。我如何做到这一点,仍然保持我的模型为对象属性访问?

相关

<一个href=\"http://stackoverflow.com/questions/11729973/how-to-create-global-instance-based-objects-from-local-scope\">How创建本地全球范围,比如基于对象?


解决方案

  

有一个简单的方法添加隐私我的模型,而且还有他们作为一个对象?

属性访问

显然,你一定要去,使整个对象私有。是的,这将作为在JavaScript中的任何其他隐私,太 - 封闭

有一个看模块模式 (有很多教程了这个在网络上)。

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

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.

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() 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?

Related

How to create global, instance based objects from local scope?

解决方案

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

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).

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

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