0

是否可以在可在派生的部分之间共享的剃刀部分中创建一个 js 对象和一些函数?

我想做类似的事情......

_BasePartial
{
   define something using js
}

...

_DerivedPartial:_BasePartial
{
   update something js
   cause _base to act on something js
} 
4

1 回答 1

1

在 BasePartial 和 DerivedPartial 视图中定义的 JS 最终在相同的页面上下文中执行,因此您的用例应该没有问题。例如:

_BasePartial:
    var something = { someVar: 2 };

    function doSomething() {
        printSomething(); //Defined on the derived view
    }

_DerivedPartial :
    function updateSomething() {
        something.someVar = 4;

        doSomething(); //Defined on the base view, should output "4" on the console
    }

    function printSomething() {
        console.log(something.someVar);
    }

SomewhereInThePage :
    //Just make sure that the BasePartial JS was executed when calling the 
    // function defined on the derived view
    updateSomething(); 
于 2012-11-17T02:54:38.110 回答