I'm trying to make the collectionView work , but I'm not really sure whether the CollectionView initiates the fetch method automatically and then populates the ChildViews with the fetched data. Or is it something I need to do manually? I have checked the Marionette doc page but I couldn't find any proper example that would answer my question.
The below example is what I've got so far , the collection is filled up with all the required data, but the data aren't passed to childview for some reason.
The below JSON is returned whenever I call 'http://localhost/GetCategories':
JSON data:
[{"category_id":"Category1","category_name":"Category One"},
{"category_id":"Category2","category_name":"Category Two"}]
collection:
define([ 'underscore', 'backbone', 'models/MainMenu/MM.model.category'], function(_, Backbone, m_Category){
var CategoriesCollection = Backbone.Collection.extend({
model: m_Category,
url : 'GetCategories';
initialize: function() {
console.log('CategoriesCollection initialized...');
this.fetch()
}
});
return CategoriesCollection;
});
collectionView:
define([ 'backbone', 'underscore', 'marionette',
'collections/MainMenu/collection.categories' ,
'layouts/MainMenu/itemview.category'],
function (Backbone, _, Marionette, c_Categories, iv_Category) {
"use strict";
var CategoriesCollectionView = Marionette.CollectionView.extend({
tagName: 'div',
template: null,
id: 'categories',
childView: iv_Category,
collection : new c_Categories(),
initialize: function(){
console.log('Categories Collection: initialized');
/// or should I call fetch() from within the CV?
/// but how do I then pass all the data to ChildView?
this.collection.fetch().done(function(){
})
},
onRender: function(){
console.log(this.collection) ///<-- shows all the JSON data properly
},
onShow: function(){
}
});
return CategoriesCollectionView;
});
ItemView:
define([ 'backbone', 'underscore', 'marionette', 'templates/template.mainmenu'], function (Backbone, _, Marionette, template) {
"use strict";
var CategoryView = Marionette.ItemView.extend({
tagName: 'div',
template: template['category.layout'],
id: 'category-layout',
initialize: function(){
console.log('Category Layout: initialized');
},
onRender: function(){
console.log(this.model) /// < --- doesn't return anything
},
onShow: function(){
console.log(this.model) ///< --- doesn't return anything
}
});
return CategoryView;
});
it only works when I create a LayoutView on top of the CollectionView and handle the fetching and assigning the collection from there.
define([ 'backbone', 'underscore', 'marionette',
'templates/template.mainmenu',
'collections/MainMenu/MM.collection.categories',
'layouts/MainMenu/collectionview.categories' ],
function (Backbone, _, Marionette, template, c_Categories, cv_Categories) {
"use strict";
var CategoryLayoutView = Marionette.LayoutView.extend({
template: template['categories.layout'],
regions: {
categories : '#categories'
},
id: 'categories-layout',
initialize: function(options){
this.collection = new c_Categories();
console.log('Category Layout: initialized');
},
onRender: function(){
var categories = this.categories
var collection = this.collection;
collection.fetch().done(function(cData){
categories.show(new cv_Categories({collection : collection}))
})
},
onShow: function(){
}
});
return CategoryLayoutView;
});
Any help would be greatly appreciated.
Thanks