backbone-forms
backbone-forms copied to clipboard
Made field instance available in template as variable
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>
'
I was trying to do the exact same thing (for the exact same reason of bootstrap input add-ons)
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().
If schema
is also passed in then custom attributes on schema can be used too, just need to look out for conflicting variable names
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.
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?
Would argue its better in the data. Are people still interested in this ?
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.
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.