Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone forms with nested models and custom template

I'm currently using Backbone forms.

I currently have a schema which loads fine using nested models. When I try and style it with a template I don't get the expected results though.

The template is similar to the following:

<div class="bounding">
  <h2>Title1 </h2>
      <div data-fields="name,type"></div>
      <div data-fields="bedrooms"></div>
  </div>
  <div class="bounding">
      <h2>Title 2</h2>
      <div data-fields="description"></div>
  </div>

Bedrooms is defined in the schema as:

bedrooms: {
  type: 'NestedModel',
  model:Bedroom,
  editorAttrs: {
    class: 'bedrooms'
  }
}

This displays correctly without the custom template which is being called by this:

template: _.template($('#formTemplate').html())

and the custom template looks correct when this line is removed

<div data-fields="bedrooms"></div>

Is there a way I can use both the custom template and a Nested Model? The Nested model has no template defined, only the has a schema added.

Thanks

Update: Js Fiddle attached the like

//  template: _.template($('#formTemplate').html()),

should be toggled to see a working way and it not looking correct

UPDATE:

Tommi Komulainen was very close with his answer Here , the description is actually in the first bounding div though rather than the second. How can I move this to the second ?

UPDATE 2:

Im calling the render of each nested item currently and injecting back in after the main render like this

form.fields.bedrooms.render();
$('#bedrooms').html(form.fields.bedrooms.el);

This is currently working but doesnt feel like a "good" solution

like image 887
exussum Avatar asked Oct 21 '22 07:10

exussum


2 Answers

Try adding a wrapper DIV tag around the whole of your template; templates need to have one main containing element.

like image 188
evilcelery Avatar answered Oct 24 '22 09:10

evilcelery


I think that <div data-fields="bedrooms"></div>should be

<div data-fieldsets="bedrooms"></div>

instead.

like image 41
Tommi Komulainen Avatar answered Oct 24 '22 11:10

Tommi Komulainen