简体   繁体   English

在Backbone和Handlebars的模型中循环一个数组

[英]Looping an array in a model with Backbone and handlebars

I'm developing an app with backbone and handlebars for the Bus timetable of my city. 我正在开发一个带有骨干和把手的应用程序,用于我所在城市的公交车时刻表。 The model of one stop is : 一站式的模型是:

    define(["jquery", "underscore", "backbone"],
      function ($, _, Backbone){

    var stop = Backbone.Model.extend({
        defaults : {
            id : "23",
            lon : "43,465187",
            lat : "-80,522372",
            listabus : ["80", "83", "106"]

        }

    });

    return stop;

});

Where "Listabus" is the list of all the bus that pass near the stop number 23. I don't know how I can loop the array in the template...help me! 其中“Listabus”是通过第23号站附近的所有公共汽车的列表。我不知道如何在模板中循环数组...帮助我! :D thanks in advice :D谢谢你的建议

This is your html: 这是你的HTML:

<!-- below is your template !-->
<script id="bus-stops" type="text/x-handlebars-template">
<ul>
    {{#each stops}}
    <li>{{this}}</li>
    {{/each}}
</ul>
</script>

<!-- result container !-->
<div id="result"></div>

And js code 和js代码

   var BusStop = Backbone.Model.extend(),
       busStop = new BusStop({stops: ['a', 'b', 'c']});
       template = $('#bus-stops').html(),
       compiler = Handlebars.compile(template),
       html = compiler({stops: busStop.get('stops')});

   $('#result').html(html);

Sorry jsfiddle won't work with handlebars 抱歉,jsfiddle不适用于车把

You just need to pass in your model attributes as an object into the underscore template function. 您只需将模型属性作为对象传递到下划线模板函数中。 The first argument is the template, the second - your data. 第一个参数是模板,第二个参数是您的数据。 You can pass in any object data but underscore plays real nice with model.toJSON() for obvious reasons. 你可以传入任何对象数据,但是下划线与model.toJSON()一起发挥得非常好,原因很明显。

this.$('#insertWherever').html(_.template($('#busList'), stopModel.toJSON()));

And your template would look something like this. 你的模板看起来像这样。

<script id="busList" type="text/html">
<ul>

  <% _.each(listabus, function(busNumber){ %>

    <li><%= busNumber %></li>

  <% }); %>

</ul>
</script>

To summarize, the <% %> is a way to escape and run arbitrary JS code. 总而言之, <% %>是一种逃避和运行任意JS代码的方法。 The <%= %> is a way to interpolate or output stuff into your template. <%= %>是一种在模板中插入或输出内容的方法。

See http://underscorejs.org/#template and http://underscorejs.org/#each http://underscorejs.org/#templatehttp://underscorejs.org/#each

If you're using require.js, you can download a plugin called text! 如果你使用的是require.js,你可以下载一个名为text的插件!

This allows you to define HTML files in your dependencies and have your templates reside in their own individual files. 这允许您在依赖项中定义HTML文件,并使模板驻留在各自的文件中。 This is opposed to the above method which uses an embedded script tag and jquery to grab the template from within whatever view you're working with. 这与上述方法相反,后者使用嵌入式脚本标记和jquery从您正在使用的任何视图中获取模板。

See plugins / text @ http://requirejs.org/docs/download.html 请参阅plugins / text @ http://requirejs.org/docs/download.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM