Backbone.js Jade 和循环模型

发布于 2024-11-27 08:49:42 字数 717 浏览 4 评论 0原文

我对使用带有玉/下划线的主干进行模板有点困惑。

我有一个主干模型,其中有几个数组,但不知道如何渲染数组属性。我可以将它们移动到一个单独的主干集合中但在这种情况下这似乎有点矫枉过正。

我按照这篇关于将backbone与jade一起使用的博客文章添加了以下内容到我的主干文件

   _.templateSettings = {
      interpolate : /\{\{(.+?)\}\}/g
   };

,它允许我在这个庄园中渲染模型属性:

      //in my JavaScript
      this.template = _.template($("#some-template").html());

      //in my .jade template
      input.text(type='text', name="name", value='{{name}}')

我想要解决的是如何对模型中的数组之一进行简单的循环。例如

    - for (var child in children)
        {{child}}

,但我对正确的语法感到非常困惑,其中玉开始并下划线接管等。谢谢。

I am getting a little confused about templating using backbone with jade/underscore.

I have a backbone model with a couple of arrays in it and am not sure how to render the array attributes. I could move them into a separate backbone collection & view but that seem like overkill in this case.

I followed this blog post on using backbone with jade and added the following to my backbone file

   _.templateSettings = {
      interpolate : /\{\{(.+?)\}\}/g
   };

which allows me to render the model attributes in this manor :

      //in my JavaScript
      this.template = _.template($("#some-template").html());

      //in my .jade template
      input.text(type='text', name="name", value='{{name}}')

what I want to work out is how to do a simple loop over one of the arrays in the model. e.g.

    - for (var child in children)
        {{child}}

but im quite confused about the correct syntax, where jade starts and underscore takes over etc. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声无音无过去 2024-12-04 08:49:42

你不能在浏览器中使用jade(从技术上来说你可能可以,但与下划线相反,与主干一起使用并不常见)。您将在那里使用下划线模板。 _.template 上的文档表明您可以评估 javascript 并使用 _.each 方法来循环模型的数组属性。

它最终会在视图的 render 函数中看起来像这样。为了提高效率,您需要将模板函数缓存为视图的属性,但为了简单起见,我将其内联在这里。例如,假设您有一个 Car 模型,其中包含一个 drivers 列表作为驱动程序名称数组。

var template = "<% _.each(model.drivers, function(name) { %> <li><%= name %></li> <% }); %>";
return _.template(template, this);

请注意,您需要在模板设置中提供 evaluate 语法,因为此示例同时包含插值样式 (<%=) 和评估样式 (<%) 模板标记。目前,您只有胡须样式插值,但这还不够。

You can't use jade in the browser (Well you probably technically can but it's not that common to use with backbone as opposed to underscore). You'll be using underscore templates there. The docs on _.template show that you can evaluate javascript and use the _.each method to loop over your model's array attributes.

It'll end up looking like this inside your view's render function. You'll want to cache the template function as an attribute of your view for efficiency, but I have it inline here for simplicity. Assume for example you have a Car model with a list of drivers as an array of driver names.

var template = "<% _.each(model.drivers, function(name) { %> <li><%= name %></li> <% }); %>";
return _.template(template, this);

Note that you will need to provide an evaluate syntax in your template settings as this example includes both the interpolate style (<%=) and the evaluate style (<%) of template markup. Currently you just have mustache style interpolation and that isn't sufficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文