user2600769
user2600769

Reputation: 155

ItemView won't render

I am using Backbone, Marionette and RequireJS to make an application, but I have trouble with Marionette's ItemView. I am trying to render a template, but an error is always thrown.

define(function(require, exports, module) {
var Marionette = require('marionette'),
    Backbone = require('backbone');

var loadModule = Marionette.Module.extend({
    initialize: function() {
        console.log('Load module');
    }
});

var template = '<div><%= message %></div>'
var ItemView = Backbone.Marionette.ItemView.extend({    
    region: '#main',
    template: template,
    model: {message: 'test'}
});

var view = new ItemView();
view.render();

return loadModule;
});

And my html file contains a div with id main. When I run the code above I get the error: 'Uncaught TypeError: Cannot read property 'apply' of undefined' when view.render() is called.

Any suggestions would help!

Upvotes: 1

Views: 2088

Answers (1)

kinakuta
kinakuta

Reputation: 9037

You're not passing a model to your view on instantiation. When Marionette tries to render, it expects to pass data from the model to the template function (in this case underscores _.template()). Since your template is referencing some data:

var template = '<div><%= message %></div>'

in this case "message", it won't be able to interpolate the value. Generally speaking what you would do on instantiation would look something like this:

var myModel = new MyModel();

var view = new ItemView({ model: myModel });

Upvotes: 2

Related Questions