代码之家  ›  专栏  ›  技术社区  ›  Prestaul

使用Jaxer时定义对象

  •  2
  • Prestaul  · 技术社区  · 17 年前

    我一直在玩 Jaxer

    我希望能够定义一个对象,并指定哪些方法在服务器上可用,哪些方法在客户端可用,哪些方法在客户端可用,但在服务器(服务器代理)上执行。 <script &燃气轮机;具有不同 runat

    function Person(name) {
        this.name = name || 'default';
    }
    Person.runat = 'both';
    
    Person.clientStaticMethod = function () {
        log('client static method');
    }
    Person.clientStaticMethod.runat = 'client';
    
    Person.serverStaticMethod = function() {
        log('server static method');
    }
    Person.serverStaticMethod.runat = 'server';
    
    Person.proxyStaticMethod = function() {
        log('proxy static method');
    }
    Person.proxyStaticMethod.runat = 'server-proxy';
    
    Person.prototype.clientMethod = function() {
        log('client method');
    };
    Person.prototype.clientMethod.runat = 'client';
    
    Person.prototype.serverMethod = function() {
        log('server method');
    };
    Person.prototype.serverMethod.runat = 'server';
    
    Person.prototype.proxyMethod = function() {
        log('proxy method');
    }
    Person.prototype.proxyMethod.runat = 'server-proxy';
    

    此外,假设我能够做到这一点,我将如何将其正确地包含到html页面中?

    1 回复  |  直到 11 年前
        1
  •  2
  •   Musa Haidari cmaduro    11 年前

    我在Aptana论坛(网络上已不存在)上发现一条帖子,上面写道: 只能代理全局函数 ... 真倒霉

    <script> 标记为 runat

    例如,我可以创建名为 Person.js.inc

    <script runat="both">
    
        function Person(name) {
            this.name = name || 'default';
        }
    
    </script>
    
    <script runat="server">
    
        Person.prototype.serverMethod = function() {
            return 'server method (' + this.name + ')';
        };
    
        Person.serverStaticMethod = function(person) {
            return 'server static method (' + person.name + ')';
        }
    
        // This is a proxied function.  It will be available on the server and
        // a proxy function will be set up on the client.  Note that it must be 
        // declared globally.
        function SavePerson(person) {
            return 'proxied method (' + person.name + ')';
        }
        SavePerson.proxy = true;
    
    </script>
    
    <script runat="client">
    
        Person.prototype.clientMethod = function() {
            return 'client method (' + this.name + ')';
        };
    
        Person.clientStaticMethod = function (person) {
            return 'client static method (' + person.name + ')';
        }
    
    </script>
    

    我可以使用以下方法将其包含在页面上:

    <jaxer:include src="People.js.inc"></jaxer:include>
    

    不幸的是,使用这种方法,我失去了浏览器缓存客户端脚本的优势,因为所有脚本都是内联的。我能找到的唯一避免这个问题的方法是将客户端方法、服务器方法和共享方法拆分为它们自己的js文件:

    <script src="Person.shared.js" runat="both" autoload="true"></script>
    <script src="Person.server.js" runat="server" autoload="true"></script>
    <script src="Person.client.js" runat="client"></script>
    

    <script src="Person.proxies.js" runat="server-proxy"></script>
    

    注意,我使用了 autoload="true"

    推荐文章