简体   繁体   English

ClientDependency Framework和Razor,添加自定义JS

[英]ClientDependency Framework & Razor, add custom JS

I'm trying to get the hang of ClientDependency Framework. 我正在尝试摆脱ClientDependency Framework的困扰。 https://github.com/Shazwazza/ClientDependency I use it in an Umbraco website. https://github.com/Shazwazza/ClientDependency我在Umbraco网站中使用了它。

I'm having a problem with some custom javascript (not in a file) that I want to run. 我要运行的一些自定义javascript(不在文件中)有问题。

I want to run a function (which is in "functions.js"), but with a different parameter per page. 我想运行一个函数(位于“ functions.js”中),但是每页具有不同的参数。

So, I add the following to my template: 因此,我将以下内容添加到模板中:

Html.RequireJs("~/scripts/functions.js", 1);

And on my masterpage before the -tag I've added: 在我添加-tag之前的主页上:

@Html.RenderJsHere()

But where do I place my function-call? 但是我应该在哪里放置函数调用? I can't just add it to my template, because "functions.js" isn't loaded yet (it's at the bottom of my masterpage). 我不能只是将其添加到模板中,因为尚未加载“ functions.js”(它在我的主页底部)。

I've thought about creating a js-file for each call and add them to the Html.RequireJs(...) but that isn't a great solution. 我已经考虑过为每个调用创建一个js文件并将它们添加到Html.RequireJs(...),但这不是一个很好的解决方案。

Is there a way to add inline-script to the list of "JS-to-render" ? 有没有一种方法可以将内联脚本添加到“ JS渲染”列表中?

edit: I was just trying to get it to work using RenderSection(), but that doesn't seem to work when the section is defined on a macro? 编辑:我只是想使用RenderSection()使它工作,但是当在宏上定义该节时,这似乎不起作用?

edit: I don't have the code here at the moment I'm typing this, but the idea is like this: 编辑:我现在输入的代码不在这里,但是这个想法是这样的:

functions.js functions.js

function WriteToConsole(input) {
    console.log('Log', input);
}

template1.cshtml template1.cshtml

@{Html.RequireJs("functions.js");}
<script>
    WriteToConsole("This is from template 1");
</script>

template2.cshtml template2.cshtml

@{Html.RequireJs("functions.js");}
<script>
    WriteToConsole("This is from template 2");
</script>

master.cshtml master.cshtml

<body>
    @RenderBody()

    @Html.RenderJsHere()
</body>

Just to give an idea of what I'm trying to do. 只是为了让我了解我要做什么。 As you can imagine, the <script> part on my template is now being called before functions.js is included. 可以想象,现在在包含functions.js之前调用了我模板上的<script>部分。 And this results in an error. 这会导致错误。

Or am I handling this whole thing wrong? 还是我把这件事搞错了?

Are you trying to alter the script call in: Html.RequireJs("~/scripts/functions.js", 1); 您是否要在以下位置更改脚本调用: Html.RequireJs("~/scripts/functions.js", 1); ?

So something like Html.RequireJs("~/scripts/functions.js?myparam=xyz", 1); 所以像Html.RequireJs("~/scripts/functions.js?myparam=xyz", 1); Is this what you are trying to achieve but having the url be dynamic? 这是您要达到的目标,但具有动态的网址吗?

If so you could do something like this : 如果是这样,您可以执行以下操作:

//perhaps have some logic above to determine what the query should be and concatenate it to the string like so.
string query = "?myparam=xyz";
string scriptcall = "~/scripts/functions.js"+query ;
Html.RequireJs(scriptcall, 1);

Could you provide more code so we can see what you are trying to do? 您能否提供更多代码,以便我们可以看到您正在尝试做什么? Maybe list in steps on how it should work? 也许列出了应该如何工作的步骤?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM