问题
Its been a long day and this is probably extemely simple but how do i add a name attribute to a Ember.TextField ?
I want to do something like this :
{view Ember.TextField valueBinding="your_name" placeholder="Your name" name="your_name"}}
This works with the placeholder but ignores the name.
Any ideas ??
thanks Rick
回答1:
The name attribute is not bound by default, see
As a workaround, you could create your own TextField, see http://jsfiddle.net/fkPzr/
App.TextField = Ember.TextField.extend({
init: function() {
this._super();
// get attributeBindings and add 'name'
var attributeBindings = this.get('attributeBindings');
attributeBindings.pushObject('name');
this.set('attributeBindings', attributeBindings);
}
});
UPDATE:
Since attributeBindings is declared as a concatenated property* the TextField can be simplified, see http://jsfiddle.net/cRhcg/:
App.TextField = Ember.TextField.extend({
attributeBindings: ['name']
});
*a concatenated property does not override the property of an extended object but the existing values from the super object are concatenated. Makes sense? Here's an example
回答2:
It doesn't work because Ember.TextField does not expose this a attribute binding.
From Ember.TextField
attributeBindings: ['type', 'value', 'size'],
plus the ones from the mixin:
attributeBindings: ['placeholder', 'disabled', 'maxlength'],
To bind the name property, create an own subclass that adds 'name' to the attribute bindings
回答3:
This will solve your problem. However, I would suggest not doing this. The idea behind ember is to keep information such as this out of the DOM.
Ember.TextField.reopen({
attributeBindings: ['name']
});
回答4:
If you simple want to have the option to specify a name attribute you can do it using attributeBindings.
{view Ember.TextField valueBinding="your_name" placeholder="Your name" attributeBindings="name" name="your_name"}}
来源:https://stackoverflow.com/questions/9894335/add-a-name-attribute-to-a-ember-textfield