Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a way to provide a data object to the component apart from the schema #16

Merged
merged 2 commits into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}`);
});