web2py:在LOAD(ajax)中使用函数

| 是否可以将= LOAD(...)与函数而不是控制器/函数字符串一起使用 例如:
Controller:
def test():
    print \"test\"

def index():
    return dict(test=test)
视图:
{{=LOAD(test, ajax=True)}}
而不是: 视图:
{{=LOAD(\'controller\', \'test\', ajax=True)}}
主要原因是,我想使用无法通过这种方式访问​​的lambda /生成的函数。     
已邀请:
        不能。但是不是因为不支持该语法,因为在逻辑上是不可能的:LOAD()是在与执行lambda的http请求不同的http请求中执行的,因此未定义后者。此外,要执行ajax回调,被调用函数必须具有名称,不能为lambda。我们可以创造性地使用缓存,以便LOAD将lambda存储在缓存中:
def callback():
    \"\"\" a generic callback \"\"\"
    return cache.ram(request.args(0),lambda:None,None)(**request.vars)

def LOAD2(f,vars={}):
    \"\"\" a new load function \"\"\"
    import uuid
    u = str(uuid.uuid4())
    cache.ram(u,lambda f=f:f,0)
    return LOAD(request.controller,\'callback\',args=u,vars=vars,ajax=True)

def index():
    \"\"\" example of usage \"\"\"
    a = LOAD2(lambda:\'hello world\')
    return dict(a=a)
但这仅适用于cache.ram,并且需要定期清除缓存。     

要回复问题请先登录注册