backbone-forms icon indicating copy to clipboard operation
backbone-forms copied to clipboard

Made field instance available in template as variable

Open gucki opened this issue 12 years ago • 8 comments

I need to add a dynamic bootstrap add-on to one of my text inputs. As I didn't find a way on how to access "custom attributes" of the field from inside the templates I updated the code to also pass the field instance to the templates. Once can now do something like:

Inside the backbone view:

@model.someField = "lala"

Custom template:

Backbone.Form.setTemplates
  'field-with-addon': '
    <div class="control-group field-{{key}}">
      <label class="control-label" for="{{id}}">{{title}}</label>
      <div class="controls">
        <div class="input-append">
          {{editor}}<span class="add-on">{{field.model.someField}}</span>
        </div>
        <div class="help-block">{{help}}</div>
      </div>
    </div>
  '

gucki avatar Aug 20 '12 22:08 gucki

I was trying to do the exact same thing (for the exact same reason of bootstrap input add-ons)

philfreo avatar Sep 27 '12 06:09 philfreo

Would it make sense to pass in a data object instead of the field? If based on a model it would be the output from model.toJSON().

powmedia avatar Oct 12 '12 12:10 powmedia

If schema is also passed in then custom attributes on schema can be used too, just need to look out for conflicting variable names

powmedia avatar Oct 12 '12 12:10 powmedia

Rather than relying on model toJSON (sometimes which doesn't include every attribute if you don't want the server to see it) I'd rather put an extra options/values in the schema, and pass the schema. I'd want a schema attribute called addonAppend, or similar.

Originally I was thinking I wanted a reusable editor (which would always use a template) for bootstrap input add-on (input-prepend and input-append). Not sure if that makes more sense, or just a custom template.

philfreo avatar Oct 12 '12 15:10 philfreo

I would agree that doing this in data would make more sense. Are we still still interested in pushing something like this in or can we close it?

reustle avatar Feb 14 '14 23:02 reustle

Would argue its better in the data. Are people still interested in this ?

exussum12 avatar Feb 06 '16 17:02 exussum12

If I'm understanding what you mean by data, I don't think that's where it always belongs, since the data isn't always specific per model. It's usually specific per field. But as long as you can specify arbitrary/custom fields at the schema/field level then the specifics don't matter very much.

philfreo avatar Feb 08 '16 02:02 philfreo

I don't like the idea of putting additional things to do with view control in the model attributes, but maybe we could have some kind of Forms decorator for the model that can be used to hint for fields? I expect the schema can be tweaked to do this also, so that may be a better option as this is what drives the forms.

glenpike avatar Feb 08 '16 09:02 glenpike