This time I'm struggling with the different methods to bind events. I've all the mentioned methods in my code. I just don't know, if I'm on the right way. Maybe I should always use bindTo to ensure that my views are closed completely after a change (at present this would often yield errors)? Are there any best practices which will help me to get in the right direction?
To illustrate my current understanding of Marionette, here's one module from my app. As always, every hint is very welcome.
PP.module('Grid', function(Grid, PP, Backbone, Marionette, $, _){
Grid.Product = Backbone.Model.extend({});
Grid.ProductCollection = Backbone.Collection.extend({
model: Grid.Product,
url: '/products/query'
});
Grid.ProductView = Backbone.Marionette.ItemView.extend({
className: 'grid',
template: 'public/templates/grid-template'
});
// Helper Methods
// -----------------------
var getGenderCode = function(genderName){
var genderMap = {
'men': 'M',
'women': 'W',
'unisex': 'A'
}
return genderMap.genderName;
}
// Public API
// -------------------
Grid.renderGrid = function(productCollection){
Grid.productView = new Grid.ProductView({
collection: productCollection
});
Grid.productView.bind('show', function(){
$('#isotope-container').isotope({
itemSelector : '.item',
containerStyle: {
position: 'relative',
zIndex: 1
}
});
});
PP.Layout.mainRegion.show(Grid.productView);
}
// Event Handlers
// -----------------------
PP.vent.bind('grid:requested', function(categoryData){
// use bootstrapped data on first start
if (PP.App.bootstrappedCategoryName !== categoryData.categoryName) {
Grid.productCollection.fetch({
data: {
gender: getGenderCode(categoryData.categoryName),
category: categoryData.categoryId
}
});
}
else {
PP.vent.trigger('mainview:ready', {
categoryName: PP.App.bootstrappedCategoryName
});
}
});
// Initializer
// --------------------
PP.addInitializer(function(options){
Grid.productCollection = new Grid.ProductCollection();
Grid.productCollection.on('reset', function(){
Grid.renderGrid(Grid.productCollection);
});
Grid.productCollection.reset(options.newArrivalsList);
});
});