将framework7的handlebar变量传递给函数

时间:2018-08-22 17:46:13

标签: javascript html-framework-7

在虚拟列表中,我有一个名为duration的变量,以毫秒为单位。 我有一个简单的函数,可以将其转换为minutes:seconds

如何将变量传递给函数?

       function success(list){
          var virtualList = app.virtualList.create({
            // List Element
            el: '.virtual-list',
            // Pass array with items
            items: list,
            // List item Template7 template
            itemTemplate:
              '<li>' +
                '<a href="#" class="item-link item-content">' +
                  '<div class="item-inner">' +
                    '<div class="item-title-row">' +
                      '<div class="item-title">{{name}}</div>' +

                      // How to pass handlebar variable?
                      '<div class="item-after">'+millisToMinutesAndSeconds(parseInt('+{{duration}}+', 10))+'</div>' +
                    '</div>' +
                    '<div class="item-title-row">' +
                      '<div class="item-subtitle">{{artist}}</div>' +
                    '</div>' +

                  '</div>' +
                '</a>' +
              '</li>',
            // Item height
            height: app.theme === 'ios' ? 63 : 73,
          });
        }

        function millisToMinutesAndSeconds(millis) {
          var minutes = Math.floor(millis / 60000);
          var seconds = ((millis % 60000) / 1000).toFixed(0);
          return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
        }

如您所见,那将行不通。如何获取值并将其传递给函数millisToMinutesAndSeconds

1 个答案:

答案 0 :(得分:1)

您可以通过构建这样的自定义帮助程序来实现:

/* You can use Handlebars.registerHelper...etc if its hbs  */
Template7.registerHelper('millisToMinutesAndSeconds', function(millis){
    var minutes = Math.floor(millis / 60000);
      var seconds = ((millis % 60000) / 1000).toFixed(0);
      return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
});

,并且您的路线将如下所示:

'.....<div class="item-after">{{millisToMinutesAndSeconds yourMillisValue}}</div>'....