使用 jQuery 加载 Telerik MVC 脚本
我正忙于构建我的第一个使用 Telerik MVC 组件的 MVC 应用程序。他们的文档指定在视图底部调用 ScriptRegistrar 帮助程序,例如“在母版页的末尾”。我假设这会呈现一个只能在页面加载时运行的脚本块。我通常更喜欢使用 jQuery 来实现此目的,并将所有与脚本相关的内容保留在母版页的顶部,最好是在 标记中。我可以做些什么来使用 Telerik 组件实现此目的,并消除母版页底部的孤独且被遗忘的帮助程序调用吗?
I'm busy building my first MVC app that uses the Telerik MVC components. Their docs specify that the ScriptRegistrar
helper be called right at the bottom of a view, e.g. "at the end of the master page.". I assume this renders a script block that must only run when the page has loaded. I normally prefer to achieve this using jQuery, and keep all my script related stuff at the top of my master page, preferably in the <head>
tag. Is there anything I can do to achieve this with the Telerik components and do away with the lone and forgotten helper call at the bottom of my master page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ScriptRegistrar 有两个主要职责:
这里有一个解释,说明为什么 ScriptRegistrar 需要位于页面的最后。之所以如此,是因为所有 UI 组件都必须在渲染期间向其注册。由于缺乏任何生命周期(顺便说一句,这是一件好事),ScriptRegistrar 必须最后出现(从而呈现)。否则,UI 组件将注册得太晚 - ScriptRegistrar 已经吐出其输出。
要回答您的问题 - 是的,您可以省略 ScriptRegistrar 并自行包含所需的 JavaScript 文件。但是,您随后需要手动初始化 UI 组件。后者是可能的,但没有记录。
The ScriptRegistrar has two main responsibilities:
There is an explanation why the ScriptRegistrar needs to be last in the page. This is so because all UI components must register with it during rendering. Due the lack of any life-cycle (which is a good thing by the way) the ScriptRegistrar must appear (thus render) last. Otherwise the UI components would register too late - the ScriptRegistrar would have spit its output already.
To answer your question - yes you can omit the ScriptRegistrar and include the required JavaScript files by yourself. However you then need to manually initialize the UI components. The latter is possible but undocumented.
ScriptRegister 块生成的代码
没有提供任何有关随后呈现的脚本的线索。然而,FireFox 的快速峰值显示正在加载 jQuery。我建议您从 scriptregister 块开始,然后使用 Firebug 找出它的调用内容,然后尝试将其全部替换为 jQuery include(以及其他脚本,如果它们被选中)。
The code generated by the ScriptRegister block is along the lines of
which offer no clues as to what scripts are being then rendered. However, a quick peak with FireFox shows that that is loading jQuery. I'd suggest you start with the scriptregister block and then use Firebug to figure out what its calling, then try swapping it all out for a jQuery include (and other scripts if they get picked up).