0

In my backbone app I have a view that needs to be rendered using different templates, dependant on a variable that is passed to the view. I am using require js and want to know if there is smart way of doing this.

Here is how I would call instantiate the new view:

var templateType = 'Template1'
var view = new DetailView({model:thisModel, 'templateType': templateType}); 

Here is an example of the view:

define(['backbone', 
    'text!templates/template1.html',
    'text!templates/template2.html' ], 
    function(Backbone, 
            Template1,
            Template2){

var DetailView = Backbone.View.extend({

    tagName : 'li',
    className: 'detail-tag',        

    initialize : function(){    
        this.detailTemplate = _.template( this.options.templateType );
        this.render();          
    },

This does not give me the required result. How do I use the options value to link to the require variable - Template1? I realise I could probably set up a switch statement or something, but that seems a bit messy. Is there a neater way of doing it?

Thanks

Ste77
  • 676
  • 1
  • 7
  • 21

1 Answers1

0

I decided to pass the template to the view, instead of trying to match it up with a template included with require. I used this question as a model for my solution:

How to pass a template to a view in Backbone

Community
  • 1
  • 1
Ste77
  • 676
  • 1
  • 7
  • 21