Using Ember.js text field ids for a

后端 未结 3 2162
再見小時候
再見小時候 2020-12-15 19:13

A label tag is of the form:

W

相关标签:
3条回答
  • 2020-12-15 19:21

    This won't always solve your problem, but I just wanted to add that simply nesting the input inside the label is often a convenient solution, since it allows you to drop the for attribute altogether (reference).

    0 讨论(0)
  • 2020-12-15 19:23

    First off, the Metamorph tag is for the label's value. It's not related to the field's id. However, this code still won't work because standard properties don't do anything special with property paths. In this case, the value of id, is literally content.field_id. This is certainly not what you wanted. In normal circumstances, you could use elementIdBinding (id is just an alias for elementId), however the element ids for Ember Views cannot be changed after creation so that approach won't work here.

    One possible solution makes use of the viewName property. The viewName property provides a named reference to the view on the parentView. You could then, do the following:

    <label {{bindAttr for="view.textField.field_id"}}> {{content.label}}</label>
    {{view Ember.TextField valueBinding="content.data" viewName="textField"}}
    
    0 讨论(0)
  • 2020-12-15 19:42

    Here was my solution to this problem from a while back:

    App.Widget.TextField = Em.ContainerView.extend({
        tagName: '',
        type: 'text',
        label: null,
        value: null,
        valueSize: '30px',
    
        childViews: ['labelView', 'inputView'],
    
        labelView: Em.View.extend({
            tagName: 'label',
            attributeBindings: ['for'],
    
            forBinding: 'parentView.inputView.elementId',
    
            init: function () {
                this.set('defaultTemplate', Em.Handlebars.compile(this.getPath('parentView.label')));
                this._super();
            }
        }),
    
        inputView: Em.TextField.extend({
            typeBinding: 'parentView.type',
            sizeBinding: 'parentView.valueSize',
            valueBinding: 'parentView.value'
        })
    });
    

    Then from within a Handlebars template:

    {{view App.Widget.TextField label="Some Label"}}
    
    0 讨论(0)
提交回复
热议问题