我一直在玩杰克塞尔,虽然这个概念很酷,但我不知道如何定义客户机和服务器上都可用的对象。我根本找不到定义对象的例子。
我希望能够定义一个对象并指定哪些方法在服务器上可用,哪些方法在客户机上可用,哪些方法在客户机上可用,哪些方法可以在服务器(服务器-代理)上执行。可以不使用具有不同runat属性的三个单独的<script>标记来完成吗?如果可能的话,我希望能够在同一个js文件中定义我的所有方法,并且在html中用三个单独的标记内联地定义我的对象是不切实际的.
基本上,我希望能够在一个js文件中这样做:
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页面中呢?
发布于 2008-09-21 04:25:24
我在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",这样它们就可以用于被代理的函数。
https://stackoverflow.com/questions/109762
复制相似问题