放置模板视图和主干视图代码的最佳实践是什么
我正在使用 Backbone.js 创建 ASP.Net MVC 3 应用程序。我还在视图中使用 jQuery 模板。
我的问题是如何有效地组织文件?
目前,我有一个用于主干应用程序的 XXX.js 文件,以及存储在显示主干应用程序 Index.cshtml 的页面中的内嵌模板。如何将模板移动到可以包含的不同文件中,就像包含 XXX.js 一样?
App/
Scripts/
backbone.js
underscore.js
jquery-1.6.4.js
jquery.tmpl.js
myBackboneApplication.js
Views/
Home/
Index.cshtml
Controllers/
HomeController.cs
我想将模板从 Index.cshtml 移出并移至与 myBackboneApplication.js 相关的内容中。我可以将其作为文件包含在 Index.cshtml 中而不是内联。
I am creating a ASP.Net MVC 3 application using Backbone.js. I am also using the jQuery templates for the views.
My question is how do I organize the files in an efficient way?
Currently I have a XXX.js file for the backbone application and inlined templates stored in the page that shows the backbone application, Index.cshtml. How can I move the templates into a different file that I can include like I include XXX.js?
App/
Scripts/
backbone.js
underscore.js
jquery-1.6.4.js
jquery.tmpl.js
myBackboneApplication.js
Views/
Home/
Index.cshtml
Controllers/
HomeController.cs
I would like to move the templates out of Index.cshtml and into something related to myBackboneApplication.js. Something that I can include as a file inside Index.cshtml instead of inlined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以查看此相关问题。两个基本选项是:
将模板创建为字符串,并将 Javascript 文件包含在索引文件中。
在单独的文件中将模板创建为 HTML,并在构建时将它们插入到索引文件中,最有可能在
标记内。
在我当前的 Backbone.js 项目中,我使用第二个选项 - 我将所有模板保存在一个单独的文件夹中,并在 ant 构建期间将它们插入到我的索引文件中。每个模板(例如
my-view-template.html
)都插入到带有id="my-view-template"
You might check out this related question. The two basic options are:
Create your templates as strings and include the Javascript file in your index file.
Create your templates as HTML in separate files, and insert them into the index file at build time, most likely within
<script type="text/template">
tags.In my current Backbone.js project, I'm using the second option - I keep all my templates in a separate folder, and I insert them into my index file during my ant build. Each template (e.g.
my-view-template.html
) is inserted into a<script>
tag withid="my-view-template"
, and then I use jQuery to create the templates using$("#my-view-template").html()
as the template string.