0

I have this Fiddle which illustrates my issue. I'm pretty sure I have it set up correctly, but I keep seeing that Marionette won't render the view.

var aApp = new Marionette.Application();

vTestSetup = Marionette.ItemView.extend({
    template: "#tmplTest"
});

var testView = new vTestSetup();

aApp.addRegions({
    appRegion: "#dApp"
});

aApp.appRegion.attachView(testView);
aApp.appRegion.show(testView);

aApp.start();

attachView Example on JSFiddle

veector
  • 3
  • 3
  • Your JSFiddle is looking for the template in the wrong element. It should be `#sample-template` not `#tmplTest`. Once that's fixed, however, it's not clear what your problem is. If you check your console, you'll get a `Uncaught ReferenceError: contentPlacement is not defined` error. Is `vTestSetup()` a model? If so, please post it's definition. It's also not defined in your JSFiddle – Bojangles Oct 10 '13 at 16:01
  • TL;DR: Your JSFiddle is incomplete. Please complete it – Bojangles Oct 10 '13 at 16:06
  • Sorry, should be complete now. – veector Oct 10 '13 at 16:24
  • Removing the `aApp.appRegion.attachView(testview);` line makes your template render fine. You shouldn't need to `attachView()` if you're doing a `show()` – Bojangles Oct 10 '13 at 16:29
  • You're correct, my question isn't necessarily how to get a view to show, but why does attachView exist if it can't work as above. I have a more complex app where I'd like to attach views that might get shown later. I'm still trying to figure out how attachView should work. – veector Oct 10 '13 at 16:30
  • http://stackoverflow.com/questions/18117082/how-to-attach-backbone-marionette-view-to-existing-element-without-creating-extr – veector Oct 10 '13 at 16:48

1 Answers1

2

Basically, you use attachView in the case where HTML is already present on the page and you want to add Backbone-managed behavior to it. All you need to do is:

  1. Specify an el attribute indicating the selector containing the view HTML
  2. instantiate a view instance
  3. attach the view to the region

You can see a practical example in one of my blog posts (search for attachView): http://davidsulc.com/blog/2012/05/06/tutorial-a-full-backbone-marionette-application-part-1/

David Sulc
  • 25,946
  • 3
  • 52
  • 54
  • Does the HTML from the view move (or get copied) into the Region's container? – veector Oct 11 '13 at 14:45
  • No, the HTML doesn't get touched (it needs to already be present in the right place). Javascript events realted to the view simply get attached to the existing HTML. – David Sulc Oct 12 '13 at 12:39