2

I want to create a view layout that I can re-use for multiple kinds of form input controls. For example, all form inputs are going to have:

  • A label
  • Some kind of input control
  • Help text
  • A placeholder for validation error messages

This is going to be a consistent component throughout the application. The only thing that will change between instances is going to be the input component. It could be, for example, a text field, text area, select, radio buttons, or whatever.

It seems like it would be possible to extract this into some kind of template, and just swap out the input control bit:

<label {{bindAttr for=view.someId}}>{{view.label}}</label>
{{something-goes-here}}
<span class="help-inline">{{view.help}}</span>
<span class="validation">{{view.validation}}</span>

I'm not sure if this is even possible though. It looks like a view layout might be the way to go, but for some reason self-closing tags aren't permitted to have layouts, so:

{{view Ember.TextField layout=myControlLayout ...}}

is out of the question (as a side note, why is this?)

Would it be possible to do this using just views? Or would a handlebars helper be more appropriate?

My question is very similar to this one, but differs in that I want to be able to use my view with any content.

1
  • It would be nice to know how I could improve this question, if you're downvoting it. Commented Jun 1, 2013 at 16:28

1 Answer 1

2

Given that setting the layout of an input element is a no-go (I'm not exactly sure why that is either), I suggest you create a view with the layout you want and use it to wrap each input control in your template. The {{yield}} helper marks where your input control will go.

Template:

<script type="text/x-handlebars" data-template-name="input-control">
    <label {{bindAttr for=view.inputId}}>{{view.label}}</label>
    {{yield}}
    <span class="help-inline">{{view.help}}</span>
    <span class="validation">{{view.validation}}</span>
</script>

View:

App.FormElementView = Ember.View.extend({
    layoutName: 'input-control',

    // defaults
    inputId: '',
    label: 'Input:',
    help: 'Enter text above.',
    validation: ''
});

Whenever you introduce an input control, wrap it in this view. Override any defaults you want in the opening {{view}} tag.

<script type="text/x-handlebars" data-template-name="index">  
    {{#view App.FormElementView 
      label="My input:"
      help="This is where the input goes"
      inputId="my_input"
      validationBinding="validation"}}

        {{input value=value id="my_input"}}

    {{/view}}
</script>

In this case, I mapped the validation string to a computed property in my controller.

App.IndexController = Ember.Controller.extend({
    value: "",
    validation: function() {
        return $.trim(this.get('value')).length > 0 ? 'Looks good.' : 'Type something.';            
    }.property('value')
});

Here's the jsfiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.