Search code examples
javascriptbackbone.jsbackbone-layout-manager

Backbone Layout Manager - Property 'parent' of undefined


I've been following the example code shown here.

Here I'm simply trying to return a layout manager view.

define(['jquery', 'underscore', 'backbone', 'layoutmanager'], 
     function($, _, Backbone, Layout){
        'use strict';

        return Backbone.Layout.extend({
            template: '#main-layout'
        });
});

Here, I'm instantiating a new view called myView. It's being loaded using requirejs.

require(['app', 'jquery', 'myView'], function (app, $, myView) {
    'use strict';
    var myView = new myView();
    myView.$el.appendTo(".container");
    myView.render();
});

Rendering the view causes the following error to pop up in the console:

Uncaught TypeError: Cannot read property 'parent' of undefined

Solution

  • Instead of myView.$el.appendTo(".container"); you should insert the view into a parent view:

    var myView = new myView();
    var container = Layout.getView('.container');
    container.insertView(myView);
    myView.render();