我在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"