Note that there are some explanatory texts on larger screens.

plurals
  1. POmulti level ul li backbone.js
    primarykey
    data
    text
    <p>I am try to making nested navigation using backbone.js below is my code.</p> <p>Json Data :</p> <pre><code>[ {"id":1,"name":"one","parent_id":0}, {"id":2,"name":"two","parent_id":0}, {"id":3,"name":"three","parent_id":0}, {"id":4,"name":"four","parent_id":0}, {"id":5,"name":"five","parent_id":0}, {"id":6,"name":"one","parent_id":2}, {"id":7,"name":"two","parent_id":2}, {"id":8,"name":"three","parent_id":2}, {"id":9,"name":"four","parent_id":2}, {"id":10,"name":"five","parent_id":2}, ] </code></pre> <p>Html :</p> <pre><code>&lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;one&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;two&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;one&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;two&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;three&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;four&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;five&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;three&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;four&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;five&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>and Backbonejs code here :</p> <pre><code>var MenuList = Backbone.View.extend({ tagName: 'ul', id: 'menu-trial-task', initialize: function() { this.collection = new ItemCollection(); this.collection.on('sync', this.render, this); this.collection.fetch(); }, render: function() { _.each( this.collection.models, function( item ) { this.$el.append( new MenuItem({model:item}).render().el ); }, this); $('nav').html(this.$el); return this; } }); var MenuItem = Backbone.View.extend({ tagName: 'li', initialize: function() { this.template = _.template($('#munu_itemt_view').html()); }, render: function() { this.$el.html( this.template( this.model.toJSON())); return this; } }); var menuList = new MenuList(); </code></pre> <p>and the result is :</p> <pre><code>&lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;one&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;two&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;three&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;four&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;five&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;one&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;two&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;three&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;four&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;five&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>as you can see this is not required result.</p> <p>i also tried with underscore.js but failed. Is there any good and simple way to do this ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload