Question

I have backbone working with Slim and i need to create a accordion menu out of a collection. The json file looks like this. Due to many data the file is just an example not the whole data. Each of this entrys are Moduls, Subjects.

[{
"modulnummer":"7500",
"semester":"5",
"liste":"GI",
"bezeichnung":"2D-Bildanalyse"
},
{"modulnummer":"7720",
"semester":"5",
"liste":"GI",
"bezeichnung":"3D-Animation"
},
{"modulnummer":"7100",
"semester":"5",
"liste":"MI",
"bezeichnung":"3D-Modellierung und Animation"
},
{
"modulnummer":"7510",
"semester":"5",
"liste":"GI",
"bezeichnung":"Advanced Networking"
},
{
"modulnummer":"2110",
"semester":"2",
"liste":"-",
"bezeichnung":"Algorithmen und Datenstrukturen"
},
{
"modulnummer":"1340",
"semester":"1",
"liste":"-",
"bezeichnung":"Analysis"
},
{
"modulnummer":"3250",
"semester":"3",
"liste":"-",
"bezeichnung":"Animation"
},
{
"modulnummer":"7520",
"semester":"5",
"liste":"GI",
"bezeichnung":"Anwendung der k\u00fcnstlichen Intelligenz"
},
{
"modulnummer":"7110",
"semester":"5",
"liste":"MI",
"bezeichnung":"Ausgew\u00e4hlte Kapitel der angewandten Informatik"
},
{
"modulnummer":"2120",
"semester":"2",
"liste":"-","bezeichnung":"Auszeichnungssprachen"
},
{
"modulnummer":"2130",
"semester":"2",
"liste":"-",
"bezeichnung":"Programmieren 2"
},
{
"modulnummer":"3110",
"semester":"5",
"liste":"MI",
"bezeichnung":"Automatentheorie und Formale Sprachen"
}]

I want to have a menu like this:

<ul>
   <li><a href='#'><span>Semester 1,3,5</span></a>
      <ul>
         <li><a href='#'><span>Semester 1</span></a>
             <ul>
                <li><a href='#'><span>Modul 1</span></a></li>
                <li><a href='#'><span>Modul 2</span></a></li>
                <li><a href='#'><span>Modul ...</span></a></li>
             </ul>
         </li>
         <li><a href='#'><span>Semester 3</span></a>...</li>
         <li><a href='#'><span>Semester 5</span></a>...</li>
      </ul>
   </li>
   <li><a href='#'><span>Semester 2,4,5</span></a>
      <ul>
         <li><a href='#'><span>Semester 2</span></a>
             <ul>
                <li><a href='#'><span>Modul 1</span></a></li>
                <li><a href='#'><span>Modul 2</span></a></li>
                <li><a href='#'><span>Modul ...</span></a></li>
             </ul>
         </li>
         <li><a href='#'><span>Semester 3</span></a>...</li>
         <li><a href='#'><span>Semester 5</span></a>...</li>
      </ul>
   </li>
   <li><a href='#'><span>Elective Modul</span></a>
      <ul>
         <li><a href='#'><span>List G</span></a>
             <ul>
                <li><a href='#'><span>Modul 1</span></a></li>
                <li><a href='#'><span>Modul 2</span></a></li>
                <li><a href='#'><span>Modul ...</span></a></li>
             </ul>
         </li>
         <li><a href='#'><span>List MI</span></a>...</li>
      </ul>
   </li>
</ul>

I've tried to built different json files, filtered collections and i even tried to work around with this problem in the view during the rendering. But non of my solutions worked. I need to filter the semesters 1,3,5 so the odd ones than the even (2,4,6) and then the once with a liste value (GI, MI). How can i do this without getting lost? Or better what is the best approach? Filter the collection? Implementing different views for each Semester? I'm a bit lost on that way.

My work:

window.NavListView = Backbone.View.extend({

    tagName : 'ul',

    render : function(eventName) {
        _.each(this.model.models, function(modul) {
            $(this.el).append(new NavListItemView({
                model : modul
            }).render().el);
        }, this);
        return this;
    },
    close : function() {
        $(this.el).unbind();
        $(this.el).remove();
    }
});

window.NavListItemView = Backbone.View.extend({

    tagName : "li",

    template : _.template($('#tpl-nav-list-item').html()),

    render : function(eventName) {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },

    close : function() {
        $(this.el).unbind();
        $(this.el).remove();
    }
});

window.NavView = Backbone.View.extend({

    template : _.template($('#nav-item-template').html()),

    initialize : function() {
        this.model.bind("change", this.render);

    },

    render : function(eventName) {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },
    close : function() {
        $(this.el).unbind();
        $(this.el).empty();
    }
}); 

the router

routes : {
            '' : 'home',
            "*path" : "notFound"

        },
        home : function() {
            if (!this.navList) {
                this.navList = new NavCollection();
                var self = this;
                this.navList.fetch({
                    success : function() {
                        self.navListView = new NavListView({
                            model : self.navList
                        });

                        // changeView fuer header schreiben TODO

                        $('header').append(self.navListView.render().el);
                        // console.log(self.navList);
                    }
                });
            };
        },

and the template

<script type="text/template" id="tpl-nav-list-item">
    <a class="pagechange" title="<%- bezeichnung %>" href='#module/<%- modulnummer %>'><%- bezeichnung %></a>
</script>

and the model

window.Nav = Backbone.Model.extend({
    urlRoot : "../api/menuitems",


    defaults : {
        "bezeichnung" : "",
        "modulnummer" : "",
        "semester" : "",
        "liste" : ""
    },
});

window.NavCollection = Backbone.Collection.extend({
    model : Nav,
    url : "../api/menuitems",

});
Was it helpful?

Solution

What you could do on the client side is to add filters to your collection to filter by odd/even semester number like so

MyModules = Backbone.Collection.extend({
   // other functions ...
   even: function() {
        return this.filter(function( modul) { return modul.get('semester') % 2;});
   }
});

and then get all even modules you want by

modules = new MyModules();  
// fetch data from somewhere ...  
modules.even();

If you can change the server side generation of the json data structures, you could return nested structures in the way you need. There is a backbone extension DeepModel which can handle nested attributes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top