如何在下划线模板中获取params

时间:2014-11-27 03:17:31

标签: underscore.js marionette

我想在下划线模板中获得“index”参数。

项目视图中

//模板#blockNewsHome

<% if (index==0) { %>
<a href="#detailNews/<%= catCode %>/<%= linkSeo %>">
    <span><%= title %></span>
    <time><%= publishDate %></time>
</a>
<% } else { %>
    <span><%= title %></span>
<% } %>

//查看

PageManager.module("PageApp.BlockNews", function(BlockNews, PageManager, Backbone, Marionette, $, _){

    BlockNews.NewsView = Marionette.ItemView.extend({ 
        template: "#blockNewsHome",
        tagName : "li",
    });

    BlockNews.NewsCollectionView = Marionette.CollectionView.extend({           
        template: "#blockNewsHome",
        tagName: "ul",      
        childView: BlockNews.NewsView, 
        render : function () {
            var self = this;                    
            this.$('li').each(function(i, element){             
                _.template(this.template,{"index": i});                 
            });
        }
    });
});

1 个答案:

答案 0 :(得分:1)

也许你需要&#34; viewtemplatehelpers&#34;:

<script id="my-template" type="text/html">
  I <%= percent %>% think that <%= showMessage() %>
</script>

var MyView = Backbone.Marionette.ItemView.extend({
  template: "#my-template",

  templateHelpers: function () {
    return {
      showMessage: function(){
        return this.name + " is the coolest!";
      },

      percent: this.model.get('decimal') * 100
    };
  }
});

var model = new Backbone.Model({
  name: "Backbone.Marionette",
  decimal: 1
});
var view = new MyView({
  model: model
});