Skip to content

Commit

Permalink
Merge pull request #16 from toddjordan/data-binding
Browse files Browse the repository at this point in the history
Adding a way to provide a data object to the component apart from the schema
  • Loading branch information
toddjordan committed Jan 15, 2016
2 parents 0feb9e4 + 5b669bf commit 5fb7d06
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 25 deletions.
9 changes: 6 additions & 3 deletions addon/components/dynamic-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ const DynamicForm = Ember.Component.extend({
} else {
schemaObj = schema;
}
if (this.attrs.postRender) {
const postRenderFn = this.attrs.postRender.value;
schemaObj["postRender"] = postRenderFn;
if (this.get('postRender')) {
const postRenderFn = this.get('postRender');
schemaObj.postRender = postRenderFn;
}
if (this.get('data')) {
schemaObj.data = this.get('data');
}
return schemaObj;
},
Expand Down
3 changes: 2 additions & 1 deletion tests/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"andThen",
"currentURL",
"currentPath",
"currentRouteName"
"currentRouteName",
"_"
],
"node": false,
"browser": false,
Expand Down
47 changes: 47 additions & 0 deletions tests/integration/components/data-binding-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//import Ember from 'ember';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';

const schemaObject = {
"schema": {
"title": "What do you think of Alpaca?",
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name"
},
"ranking": {
"type": "string",
"title": "Ranking",
"enum": ['excellent', 'not too shabby', 'alpaca built my hotrod']
}
}
}
};

const dataObject = {
"name": "Todd Jordan",
"ranking": "not too shabby"
};

moduleForComponent('/dynamic-form' , 'Integration | Component | dynamic-form:data-binding', {
integration: true
});

test('should update input value when model updated', function (assert) {
const fixtureSchema = _.clone(schemaObject, true);
const done = assert.async();
fixtureSchema.postRender = () => {
assert.equal(this.$('.alpaca-field-text input').val(), 'Todd Jordan');
dataObject.name = 'Jeremy Rowe';
fixtureSchema.postRender = () => {
assert.equal(this.$('.alpaca-field-text input').val(), 'Jeremy Rowe');
done();
};
this.render(hbs`{{dynamic-form schema=schemaObject data=dataObject}}`);
};
this.set('schemaObject', fixtureSchema);
this.set('dataObject', dataObject);
this.render(hbs`{{dynamic-form schema=schemaObject data=dataObject}}`);
});
39 changes: 18 additions & 21 deletions tests/integration/components/dynamic-form-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Ember from 'ember';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';

Expand All @@ -25,27 +24,25 @@ moduleForComponent('/dynamic-form', 'Integration | Component | dynamic-form:basi
});

test('should render form from valid schema object', function (assert) {
return new Ember.RSVP.Promise((resolve) => {
basicObject["postRender"] = () => {
assert.ok(this.$('.alpaca-field-text input').length, 'input field exists');
assert.ok(this.$('.alpaca-field-radio').length, 'radio group exists');
resolve();
};
this.set('basicObject', basicObject);
this.render(hbs`{{dynamic-form schema=basicObject}}`);
});
const done = assert.async();
basicObject["postRender"] = () => {
assert.ok(this.$('.alpaca-field-text input').length, 'input field exists');
assert.ok(this.$('.alpaca-field-radio').length, 'radio group exists');
done();
};
this.set('basicObject', basicObject);
this.render(hbs`{{dynamic-form schema=basicObject}}`);
});

test('should render form from valid schema string', function (assert) {
return new Ember.RSVP.Promise((resolve) => {
const postRenderFn = () => {
assert.ok(this.$('.alpaca-field-text input').length, 'input field exists');
assert.ok(this.$('.alpaca-field-radio').length, 'radio group exists');
resolve();
};
const basicString = JSON.stringify(basicObject);
this.set('postRenderFn', postRenderFn);
this.set('basicString', basicString);
this.render(hbs`{{dynamic-form schema=basicString postRender=postRenderFn}}`);
});
const done = assert.async();
const postRenderFn = () => {
assert.ok(this.$('.alpaca-field-text input').length, 'input field exists');
assert.ok(this.$('.alpaca-field-radio').length, 'radio group exists');
done();
};
const basicString = JSON.stringify(basicObject);
this.set('postRenderFn', postRenderFn);
this.set('basicString', basicString);
this.render(hbs`{{dynamic-form schema=basicString postRender=postRenderFn}}`);
});

0 comments on commit 5fb7d06

Please sign in to comment.