2
votes

I'm converting my backbone example to now extend Marionette. I'm finding it difficult understand achieving the same thing with templates. Let me explain.

This is how I use to render a template

In view render function:

campaign.fetch({
 var template = _.template(campaignTemplate, {campaign: campaign});
 that.$el.html(template);  

With backbone.marionette I'm not sure how to do the same thing, this is what I have tried without any joy:

  var campaginView = Backbone.Marionette.ItemView.extend({



        initialize: function (options) {
            // campaign id passed from the URL Route
            this.campaign_id = options.id;
        },

        model: new CampaginModel({
            id: this.campaign_id
        }),

        template: campaignTemplate({
           campaign: this.model.fetch() 
        }),



    }); // end campagin view

*What I'm I doing wrong? underscore is not even there! *

1

1 Answers

8
votes

Marionette does not require you to pass the model to the template, that is a repetitive task that you need to do in all the Backbone views, and One of the ideas behind Marionette is to reduce boilerplate code.

 var campaginView = Backbone.Marionette.ItemView.extend({

    initialize: function (options) {
        // campaign id passed from the URL Route
        this.campaign_id = options.id;
        this.model = new CampaingModel({id:this.campaign_id});
        this.model.fetch();
    },
    template: campaignTemplate,

});

A great resource of clear and concise examples is the Marionette documentation, please take a look a this link https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.itemview.md

also I created a small jsfiddle for this

http://jsfiddle.net/rayweb_on/msWvV/