0
votes

I'm using Marionette Views within Backbone.

In my template I would like to pull out the name attribute from my models fetched data. The model has been populated and I can see via console.log the correct results, but when I try to output this in a template:

<script type="text/html" id="sample-template">
<h1><%= campaign ? campaign.get('name') : '' %></h1>
</script>

I get the message: campaign is not defined I have also tried attributes.get('name'), CampaginModel.get('name') and model.get('name') but get the same error

Does Marionette have a different of of doing this, if so how should I do it?

view:

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

        initialize: function (options) {
            this.campaign_id = options.id;
            this.model.fetch();
        },

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

        template: campaignTemplate,

    }); 

console.log of model:

_changing: false
_pending: false
_previousAttributes: Object
attributes: Object
      modified: "2013-11-08T18:57:44"
      name: "test"
      user: Object
__proto__: Object
changed: Object
cid: "c2"
__proto__: s
1

1 Answers

1
votes

Based on a short look into the marionette documentation i bet you have a serialized json representation of your models attributes passed to your template and not the model object itself, try this:

<script type="text/html" id="sample-template">
<h1><%= name ? name : '' %></h1>
</script>

Think of it like the value passed to your template is:

model.toJSON()

See ItemView serializeData