I'm trying to implement breadcrumb using Marionette.CollectionView
and Marionette.ItemView
. I want to add breadcrumbs to the list from any part of the module.
Here is the module which I wrote:
App.module("BreadcrumbModule", function(BreadcrumbModule){
var instance;
var Breadcrumb = Backbone.Model.extend({
defaults:{
link : '',
name : ''
}
});
BreadcrumbModule.BreadcrumbList = Backbone.Collection.extend({
model : Breadcrumb
});
var BreadcrumbView = Marionette.ItemView.extend({
tagName : 'li',
render : function() {
var liData = '';
if(this.model.get('link') == '#') {
liData = 'You are here: ';
} else {
liData = '<a href="#' + this.model.get('link') + '" class="tabSection">' + this.model.get('name') + '</a>';
}
$(this.el).html(liData);
return this;
}
});
BreadcrumbModule.BreadcrumbListView = Marionette.CollectionView.extend({
tagName: "ul",
id: "Breadcrumb",
itemView: BreadcrumbView,
initialize : function() {
this.collection = new BreadcrumbModule.BreadcrumbList();
this.collection.bind('add', this.appendBreadcrumb);
//this.collection.add({link: '#', name: 'You are here : '});
this.listenTo(this.collection, "add", this.appendBreadcrumb);
},
addBradcrumb: function(breadcrumbList){
breadcrumbList.forEach(function(breadcrumb) {
//console.log(breadcrumb);
this.collection.add(breadcrumb);
}, this);
},
appendBreadcrumb: function(breadcrumb) {
var breadcrumbView = new BreadcrumbView({
model : breadcrumb
});
$(this.el).append(breadcrumbView.render().el);
}
});
// Singleton
BreadcrumbModule.getInstance = function() {
if(!BreadcrumbModule.instance) {
instance = new BreadcrumbModule.BreadcrumbListView();
}
return instance;
}
});
My plan to access this module as follows:
//var breadcrumbs = [];
//breadcrumbs.push({link: 'book', name: 'Book'});
//var breadcrumbListView = App.BreadcrumbModule.getInstance();
var breadcrumb = new App.BreadcrumbModule.Breadcrumb({link: '#', name: 'You are here: '});
var breadcrumbList = new App.BreadcrumbModule.BreadcrumbList();
breadcrumbList.add(breadcrumb);
breadcrumbListView.addBradcrumb(breadcrumbs);
I can see the default one is showing("You are here:") and the 'collection' is growing when I add new breadcrumbs but its not reflecting in the view.
What could be the reason?
How can I fix it?
Thank you!
//San.
breadcrumbs
) that you then pass on to a method on the CollectionView instead of just adding a new model to the collection itself? Your CollectionView should justlistenTo
the collection changing and re-render. – christian314159