Permalink
Browse files

Add select support

  • Loading branch information...
1 parent d7bf24d commit ce0a74683467e1306036c16c095f52e021fab1e6 @josepjaume josepjaume committed Jun 12, 2012
View
14 Assetfile
@@ -59,6 +59,20 @@ input "packages" do
end
input "packages" do
+ match "*/lib/**/*.js" do
+ minispade :rewrite_requires => true, :string => true, :module_id_generator => proc { |input|
+ id = input.path.dup
+ id.sub!('/lib/', '/')
+ id.sub!(/\.js$/, '')
+ id.sub!(/\/main$/, '')
+ id
+ }
+
+ concat "ember-forms-spade.js"
+ end
+end
+
+input "packages" do
match "*/lib/**/main.js" do
neuter(
:additional_dependencies => proc { |input|
View
2 dist/ember-forms-spade.js
1 addition, 1 deletion not shown because the diff is too large. Please use a local Git client to view these changes.
View
32 dist/ember-forms.js
@@ -77,10 +77,16 @@ EF.BaseField = Ember.View.extend({
),
setFormView: function(){
- var formView = findFormRecursively(this.get('parentView'));
- formView.get('fieldViews').pushObject(this);
- this.set('formView', formView);
- this.set('content', formView.get('content'));
+ var parentView, formView;
+
+ if(parentView = this.get('parentView')){
+ formView = findFormRecursively(parentView);
+ }
+ if(formView){
+ formView.get('fieldViews').pushObject(this);
+ this.set('formView', formView);
+ this.set('content', formView.get('content'));
+ }
},
bindValue: function(){
@@ -127,6 +133,24 @@ EF.TextareaField = EF.BaseField.extend({
(function() {
+EF.SelectField = EF.BaseField.extend({
+ optionLabelPath: 'content.name',
+ optionValuePath: 'content.id',
+ InputView: Ember.Select.extend({
+ init: function(){
+ this.set('optionLabelPath', this.getPath('parentView.optionLabelPath'));
+ this.set('optionValuePath', this.getPath('parentView.optionValuePath'));
+ this._super();
+ },
+ contentBinding: 'field.content'
+ })
+});
+
+})();
+
+
+
+(function() {
/*
* Usage:
*/
View
2 dist/ember-forms.min.js
@@ -6,4 +6,4 @@
-(function(){window.EF=Ember.Namespace.create({findFormRecursively:function(a){var b=a;do if(b.get("isForm")===!0)return b;while(b=a.get("parentView"))},findFieldRecursively:function(a){var b=a;do if(b.get("isField")===!0)return b;while(b=a.get("parentView"))},findField:function(a){a=a||"text";var b=Ember.String.camelize(a);b=b.replace(/^./,b[0].toUpperCase()),b+="Field";var c=EF[b];if(c)return c;throw"Field "+a+" cannot be found"}})})(),function(){var a=EF.findFieldRecursively;EF.Label=Ember.View.extend({tagName:"label",field:Ember.computed(function(){return a(this)}),attributeBindings:["for"],formBinding:"field.form",name:Ember.computed(function(){return this.getPath("field.label")||this.getPath("field.name")}),nameBinding:"field.label",template:Ember.Handlebars.compile("{{view.name}}"),didInsertElement:function(){Ember.bind(this,"for","component.inputView.elementId")}})}(),function(){var a=EF.findFormRecursively;EF.BaseField=Ember.View.extend({name:null,formView:null,tagName:"div",classNames:["input"],LabelView:EF.Label.extend(),InputView:null,value:null,isField:!0,template:Ember.Handlebars.compile('{{view view.LabelView viewName="labelView"}}{{view view.InputView viewName="inputView" fieldBinding="view" valueBinding="view.value" nameBinding="view.name"}}'),setFormView:function(){var b=a(this.get("parentView"));b.get("fieldViews").pushObject(this),this.set("formView",b),this.set("content",b.get("content"))},bindValue:function(){Ember.oneWay(this,"value","content."+this.get("name"))},data:Ember.computed(function(){var a={};return a[this.get("name")]=this.get("value"),a}).property("value"),init:function(){this._super(),this.setFormView(),this.bindValue()}})}(),function(){EF.TextField=EF.BaseField.extend({InputView:Ember.TextField.extend({attributeBindings:["name"]})})}(),function(){EF.TextareaField=EF.BaseField.extend({InputView:Ember.TextArea.extend({template:Ember.Handlebars.compile("{{view.value}}")})})}(),function(){EF.Form=Ember.View.extend({tagName:"form",attributeBindings:["action"],fieldViews:Ember.A(),buttons:["submit"],content:null,isForm:!0,data:Ember.computed(function(){var a={};return this.get("fieldViews").forEach(function(b){var c=b.get("data");for(var d in c)a[d]=c[d]}),a}).property("fieldViews.@each.value"),submit:function(){return this.save(this.get("data")),!1},save:function(a){}})}(),function(){EF.SubmitButton=Ember.View.extend({tagName:"button",attributeBindings:["type"],type:"submit",name:"Save",template:Ember.Handlebars.compile("{{view.name}}")})}(),function(){var a=EF.findFormRecursively,b=EF.findField;EF.ButtonHelper=Ember.Object.create({helper:function(a,b){var c=EF.SubmitButton,d=b.data.view;d.appendChild(c,b.hash)}}),Ember.Handlebars.registerHelper("form",function(a,b){if(a==="buttons")EF.ButtonHelper.helper(this,b);else throw"Unknown "+a+" in form helper"})}(),function(){var a=EF.findFormRecursively,b=EF.findField;EF.FieldHelper=Ember.Object.create({helper:function(a,c,d){var e=d.hash,f=e.as,g=d.data.view,h=b(f);Ember.empty(e.name)&&(e.name=c),delete e.as,g.appendChild(h,e)}}),Ember.Handlebars.registerHelper("field",function(a,b){EF.FieldHelper.helper(this,a,b)})}(),function(){}(),function(){}()
+(function(){window.EF=Ember.Namespace.create({findFormRecursively:function(a){var b=a;do if(b.get("isForm")===!0)return b;while(b=a.get("parentView"))},findFieldRecursively:function(a){var b=a;do if(b.get("isField")===!0)return b;while(b=a.get("parentView"))},findField:function(a){a=a||"text";var b=Ember.String.camelize(a);b=b.replace(/^./,b[0].toUpperCase()),b+="Field";var c=EF[b];if(c)return c;throw"Field "+a+" cannot be found"}})})(),function(){var a=EF.findFieldRecursively;EF.Label=Ember.View.extend({tagName:"label",field:Ember.computed(function(){return a(this)}),attributeBindings:["for"],formBinding:"field.form",name:Ember.computed(function(){return this.getPath("field.label")||this.getPath("field.name")}),nameBinding:"field.label",template:Ember.Handlebars.compile("{{view.name}}"),didInsertElement:function(){Ember.bind(this,"for","component.inputView.elementId")}})}(),function(){var a=EF.findFormRecursively;EF.BaseField=Ember.View.extend({name:null,formView:null,tagName:"div",classNames:["input"],LabelView:EF.Label.extend(),InputView:null,value:null,isField:!0,template:Ember.Handlebars.compile('{{view view.LabelView viewName="labelView"}}{{view view.InputView viewName="inputView" fieldBinding="view" valueBinding="view.value" nameBinding="view.name"}}'),setFormView:function(){var b,c;if(b=this.get("parentView"))c=a(b);c&&(c.get("fieldViews").pushObject(this),this.set("formView",c),this.set("content",c.get("content")))},bindValue:function(){Ember.oneWay(this,"value","content."+this.get("name"))},data:Ember.computed(function(){var a={};return a[this.get("name")]=this.get("value"),a}).property("value"),init:function(){this._super(),this.setFormView(),this.bindValue()}})}(),function(){EF.TextField=EF.BaseField.extend({InputView:Ember.TextField.extend({attributeBindings:["name"]})})}(),function(){EF.TextareaField=EF.BaseField.extend({InputView:Ember.TextArea.extend({template:Ember.Handlebars.compile("{{view.value}}")})})}(),function(){EF.SelectField=EF.BaseField.extend({optionLabelPath:"content.name",optionValuePath:"content.id",InputView:Ember.Select.extend({init:function(){this.set("optionLabelPath",this.getPath("parentView.optionLabelPath")),this.set("optionValuePath",this.getPath("parentView.optionValuePath")),this._super()},contentBinding:"field.content"})})}(),function(){EF.Form=Ember.View.extend({tagName:"form",attributeBindings:["action"],fieldViews:Ember.A(),buttons:["submit"],content:null,isForm:!0,data:Ember.computed(function(){var a={};return this.get("fieldViews").forEach(function(b){var c=b.get("data");for(var d in c)a[d]=c[d]}),a}).property("fieldViews.@each.value"),submit:function(){return this.save(this.get("data")),!1},save:function(a){}})}(),function(){EF.SubmitButton=Ember.View.extend({tagName:"button",attributeBindings:["type"],type:"submit",name:"Save",template:Ember.Handlebars.compile("{{view.name}}")})}(),function(){var a=EF.findFormRecursively,b=EF.findField;EF.ButtonHelper=Ember.Object.create({helper:function(a,b){var c=EF.SubmitButton,d=b.data.view;d.appendChild(c,b.hash)}}),Ember.Handlebars.registerHelper("form",function(a,b){if(a==="buttons")EF.ButtonHelper.helper(this,b);else throw"Unknown "+a+" in form helper"})}(),function(){var a=EF.findFormRecursively,b=EF.findField;EF.FieldHelper=Ember.Object.create({helper:function(a,c,d){var e=d.hash,f=e.as,g=d.data.view,h=b(f);Ember.empty(e.name)&&(e.name=c),delete e.as,g.appendChild(h,e)}}),Ember.Handlebars.registerHelper("field",function(a,b){EF.FieldHelper.helper(this,a,b)})}(),function(){}(),function(){}()
View
32 dist/ember-forms.prod.js
@@ -77,10 +77,16 @@ EF.BaseField = Ember.View.extend({
),
setFormView: function(){
- var formView = findFormRecursively(this.get('parentView'));
- formView.get('fieldViews').pushObject(this);
- this.set('formView', formView);
- this.set('content', formView.get('content'));
+ var parentView, formView;
+
+ if(parentView = this.get('parentView')){
+ formView = findFormRecursively(parentView);
+ }
+ if(formView){
+ formView.get('fieldViews').pushObject(this);
+ this.set('formView', formView);
+ this.set('content', formView.get('content'));
+ }
},
bindValue: function(){
@@ -127,6 +133,24 @@ EF.TextareaField = EF.BaseField.extend({
(function() {
+EF.SelectField = EF.BaseField.extend({
+ optionLabelPath: 'content.name',
+ optionValuePath: 'content.id',
+ InputView: Ember.Select.extend({
+ init: function(){
+ this.set('optionLabelPath', this.getPath('parentView.optionLabelPath'));
+ this.set('optionValuePath', this.getPath('parentView.optionValuePath'));
+ this._super();
+ },
+ contentBinding: 'field.content'
+ })
+});
+
+})();
+
+
+
+(function() {
/*
* Usage:
*/
View
32 dist/modules/ember-forms.js
@@ -77,10 +77,16 @@ EF.BaseField = Ember.View.extend({
),
setFormView: function(){
- var formView = findFormRecursively(this.get('parentView'));
- formView.get('fieldViews').pushObject(this);
- this.set('formView', formView);
- this.set('content', formView.get('content'));
+ var parentView, formView;
+
+ if(parentView = this.get('parentView')){
+ formView = findFormRecursively(parentView);
+ }
+ if(formView){
+ formView.get('fieldViews').pushObject(this);
+ this.set('formView', formView);
+ this.set('content', formView.get('content'));
+ }
},
bindValue: function(){
@@ -127,6 +133,24 @@ EF.TextareaField = EF.BaseField.extend({
(function() {
+EF.SelectField = EF.BaseField.extend({
+ optionLabelPath: 'content.name',
+ optionValuePath: 'content.id',
+ InputView: Ember.Select.extend({
+ init: function(){
+ this.set('optionLabelPath', this.getPath('parentView.optionLabelPath'));
+ this.set('optionValuePath', this.getPath('parentView.optionValuePath'));
+ this._super();
+ },
+ contentBinding: 'field.content'
+ })
+});
+
+})();
+
+
+
+(function() {
/*
* Usage:
*/
View
14 packages/ember-forms/lib/fields/base.js
@@ -19,10 +19,16 @@ EF.BaseField = Ember.View.extend({
),
setFormView: function(){
- var formView = findFormRecursively(this.get('parentView'));
- formView.get('fieldViews').pushObject(this);
- this.set('formView', formView);
- this.set('content', formView.get('content'));
+ var parentView, formView;
+
+ if(parentView = this.get('parentView')){
+ formView = findFormRecursively(parentView);
+ }
+ if(formView){
+ formView.get('fieldViews').pushObject(this);
+ this.set('formView', formView);
+ this.set('content', formView.get('content'));
+ }
},
bindValue: function(){
View
14 packages/ember-forms/lib/fields/select.js
@@ -0,0 +1,14 @@
+require("ember-forms/fields/base");
+
+EF.SelectField = EF.BaseField.extend({
+ optionLabelPath: 'content.name',
+ optionValuePath: 'content.id',
+ InputView: Ember.Select.extend({
+ init: function(){
+ this.set('optionLabelPath', this.getPath('parentView.optionLabelPath'));
+ this.set('optionValuePath', this.getPath('parentView.optionValuePath'));
+ this._super();
+ },
+ contentBinding: 'field.content'
+ })
+});
View
1 packages/ember-forms/lib/main.js
@@ -1,5 +1,6 @@
require("ember-forms/core");
require("ember-forms/fields/text");
require("ember-forms/fields/textarea");
+require("ember-forms/fields/select");
require("ember-forms/form");
require("ember-forms/helpers");
View
21 packages/ember-forms/tests/fields/select.js
@@ -0,0 +1,21 @@
+test("it creates a select", function() {
+ var select = EF.SelectField.create({
+ content: Ember.A([
+ {id: 1, name: 'James'},
+ {id: 2, name: 'John'}
+ ])
+ });
+
+ Ember.run(function(){
+ select.appendTo("#qunit-fixture");
+ });
+
+ equal(select.$("option").length, 2, "it is populated with two options");
+ var firstOption = select.$("option:first");
+ equal(firstOption.attr('value'), "1", "it assigns a value");
+ equal(firstOption.text(), "James", "it assigns a name");
+
+ Ember.run(function(){
+ select.destroy();
+ });
+});
View
19 packages/ember-forms/tests/integration.js
@@ -48,3 +48,22 @@ test("it populates a form with content's values", function() {
equal(form.$("textarea").val(), "rafa@capybara.com");
equal(data.email, 'rafa@capybara.com');
});
+
+test("it allows options for a select", function(){
+ var form = EF.Form.create({
+ names: Ember.A([{id: 1, name: 'John'}]),
+ template: Ember.Handlebars.compile(
+ '{{ field age as="select" contentBinding="formView.names" }}'
+ )
+ });
+
+ Ember.run(function(){
+ form.appendTo("#qunit-fixture");
+ });
+
+ equal(form.$("option:first").text(), "John");
+
+ Ember.run(function(){
+ form.destroy();
+ });
+});

0 comments on commit ce0a746

Please sign in to comment.