Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that this is the more <strong>flexible</strong> way to do this:</p> <p>Javascript</p> <pre><code>Boostrap = Ember.Namespace.create(); </code></pre> <p>To simplify the things each <code>FormControl</code> have the properties: label, message and an intern control. So you can extend it and specify what control you want. Like combobox, radio button etc.</p> <pre><code>Boostrap.FormControl = Ember.View.extend({ classNames: ['form-group'], classNameBindings: ['hasError'], template: Ember.Handlebars.compile('\ &lt;label class="col-lg-2 control-label"&gt;{{view.label}}&lt;/label&gt;\ &lt;div class="col-lg-10"&gt;\ {{view view.control}}\ &lt;span class="help-block"&gt;{{view.message}}&lt;/span&gt;\ &lt;/div&gt;'), control: Ember.required() }); </code></pre> <p>The <code>Boostrap.TextField</code> is one of the implementations, and your component is a <code>Ember.TextField</code>. Because that <code>Boostrap.TextField</code> is an instance of <code>Ember.View</code> and not an <code>Ember.TextField</code> directly. We delegate the value using <code>Ember.computed.alias</code>, so you can use <code>valueBinding</code> in the templates.</p> <pre><code>Boostrap.TextField = Boostrap.FormControl.extend({ control: Ember.TextField.extend({ classNames: ['form-control'], value: Ember.computed.alias('parentView.value') }) }); </code></pre> <p>Nothing special here, just create the defaults values tagName=form and classNames=form-horizontal, for not remember every time.</p> <pre><code>Boostrap.Form = Ember.View.extend({ tagName: 'form', classNames: ['form-horizontal'] }); </code></pre> <p>Create a subclass of <code>Boostrap.Form</code> and delegate the validation to controller, since it have to be the knowledge about validation.</p> <pre><code>App.LoginFormView = Boostrap.Form.extend({ submit: function() { debugger; if (this.get('controller').validate()) { alert('ok'); } return false; } }); </code></pre> <p>Here is where the validation logic and handling is performed. All using bindings without the need of touch the dom.</p> <pre><code>App.IndexController = Ember.ObjectController.extend({ value: null, message: null, hasError: Ember.computed.bool('message'), validate: function() { this.set('message', ''); var valid = true; if (!/^[a-z0-9]+$/i.test(this.get('value'))) { this.set('message', 'Just numbers or alphabetic letters are allowed'); valid = false; } return valid; } }); </code></pre> <p>Templates</p> <pre><code>&lt;script type="text/x-handlebars" data-template-name="index"&gt; {{#view App.LoginFormView}} {{view Boostrap.TextField valueBinding="value" label="Alpha numeric" messageBinding="message" hasErrorBinding="hasError"}} &lt;button type="submit" class="btn btn-default"&gt;Submit&lt;/button&gt; {{/view}} &lt;/script&gt; </code></pre> <p><a href="http://jsfiddle.net/marciojunior/e9fhB/" rel="nofollow">Here a live demo</a></p> <h1>Update</h1> <p>Like @intuitivepixel have said, ember-boostrap have this implemented. So consider my sample if you don't want to have a dependency in ember-boostrap.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload