Skip to content

Instantly share code, notes, and snippets.

@kkemple
Created January 31, 2015 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkemple/fb24d4499193628343c5 to your computer and use it in GitHub Desktop.
Save kkemple/fb24d4499193628343c5 to your computer and use it in GitHub Desktop.
var View = Marionette.ItemView.extend({
template: function(modelAttrs) {
// modelAttrs is raw attributes for model, not model itself
switch (modelAttrs[{some unique identifier}]) {
case 'option1':
return Option1ItemView;
case 'option2':
return Option2ItemView;
case 'option3':
return Option3ItemView;
case 'option4':
return Option4ItemView;
}
}
});
@daniel-deychakiwsky
Copy link

When you say return OptionXItemView - I think you mean return a template and not a view, correct?

@daniel-deychakiwsky
Copy link

Here is what I was thinking for my Views module, I am only exposing one method to the App:

/** 
 * @module Views
 */
define(['jquery', 'underscore', 'marionette', 'http://danieldeychakiwsky.github.io/IntegralAds/DogDiagnostic/Templates.js'], 
    function($, _, Marionette, Templates) {

        var ItemViewMap = {
            'S': Marionette.ItemView.extend({
                template: Templates.screenEventsViewTemp
            }),
            'C': Marionette.ItemView.extend({
                template: Templates.cumulativeStatesTemp
            }),
            'D': Marionette.ItemView.extend({
                template: Templates.detectionResultsTemp
            }),
            'N': Marionette.ItemView.extend({
                template: Templates.networkCallsViewTemp
            })
        };

        var AppCollectionView = Marionette.CollectionView.extend({
            // Serve the correct view for each model
            getChildView: function(item) {
                var viewKey = item.get('viewId');
                if (ItemViewMap[viewKey]) { return ItemViewMap[viewKey]; }
            }
        });

        var dispatchModelsToView = function(models) {
            var CollectionView = new AppCollectionView();
            for (modelName in models) {
                if (models.hasOwnProperty(modelName)) {
                     CollectionView.collection.add(models[modelName]);
                };
            };    
        };

        return {
            dispatchModelsToView: dispatchModelsToView
        }
    });

So I pass in my models, distribute them to the CollectionView which pulls up the necessary ItemView pending the model's viewId attribute. The only thing I am yet to get is how to display those in actual templates.

@daniel-deychakiwsky
Copy link

Ahh I think I see your point, maybe this would be bette:

/** 
 * @module Views
 */
define(['jquery', 'underscore', 'marionette', 'http://danieldeychakiwsky.github.io/IntegralAds/DogDiagnostic/Templates.js'], 
    function($, _, Marionette, Templates) {

        var TemplateMap = {
            'S': Templates.screenEventsViewTemp,
            'C': Templates.cumulativeStatesTemp,
            'D': Templates.detectionResultsTemp,
            'N': Templates.networkCallsViewTemp
        };

        var AppItemView = Marionette.ItemView.extend({
            // Serve the correct view for each model
            getTemplate: function() {
                var viewKey = this.model.get('viewId');
                if (TemplateMap[viewKey]) { return TemplateMap[viewKey]; }
            }
        });

        var AppCollectionView = Marionette.CollectionView.extend({
            childView: AppItemView
        });

        var dispatchModelsToView = function(models) {
            var CollectionView = new AppCollectionView();
            for (modelName in models) {
                if (models.hasOwnProperty(modelName)) {
                     CollectionView.collection.add(models[modelName]);
                };
            };    
        };

        return {
            dispatchModelsToView: dispatchModelsToView
        }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment