0

I'm loading a precompiled handlebars template in a layout view. I'm defining the regions, but, I get the error:

Uncaught Error: An "el" #questions-section must exist in DOM (of course that node exists in the template).

Is this happening because I'm using a precompiled template? The code:

q.boxView = Marionette.LayoutView.extend({
    tagName : 'li',
    className : "sortable_boxes_original",
    template: Handlebars.templates["repoboxview"],
    initialize : function(){
        this.createQuestionsCollection();
        this.createResponsesModel();
        this.listenTo(this.model.get("response"), 'change', this.triggerChanged);
    },
    regions: {
        questions: "#questions-section"
    },
    onBeforeRender: function(){
      var that = this;
      this.getRegion("questions").show(new q.questionsListView({collection: this.model.get("questions"), model: this.model}));
    } 
}); 
sica07
  • 4,896
  • 8
  • 35
  • 54

2 Answers2

2

It's happening because you're trying to put something in the region before the region is actually part of the DOM. From the documentation:

A region will only be able to populate itself if the View has access to the elements specified within the region definitions. That is, if your view has not yet rendered, your regions may not be able to find the element that you've specified for them to manage. In that scenario, using the region will result in no changes to the DOM.

So instead of using onBeforeRender(), try onBeforeShow().

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
2

Adding on Kevin's answer, a better suited callback for showing child views into regions is onBeforeShow()

Akshad
  • 81
  • 3
  • Good point, and I've updated my answer. This would have been better as a comment, but I see that you don't have the reputation yet to comment, so fair enough... – Kevin Christopher Henry Aug 12 '15 at 05:35
  • Yup... Felt pretty stupid to post another answer when I just wanted to add on to your correct answer. – Akshad Aug 12 '15 at 23:05