是否可以在razor partial中创建一个js对象和一些函数,这些函数可以在派生的partials之间共享?
我想做点像......
_BasePartial
{
define something using js
}
...
_DerivedPartial:_BasePartial
{
update something js
cause _base to act on something js
}
答案 0 :(得分: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();