-
Notifications
You must be signed in to change notification settings - Fork 20
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
Use template #58
base: master
Are you sure you want to change the base?
Use template #58
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,12 @@ module.exports = View.extend({ | |
} | ||
}, | ||
|
||
template: [ | ||
'<form>', | ||
'<fieldset data-hook="field-container"></fieldset>', | ||
'</form>' | ||
].join('\n'), | ||
|
||
initialize: function(opts) { | ||
opts = opts || {}; | ||
this.el = opts.el; | ||
|
@@ -50,13 +56,9 @@ module.exports = View.extend({ | |
// add all our fields | ||
(result(opts, 'fields') || result(this, 'fields') || []).forEach(this.addField, this); | ||
|
||
if (opts.autoRender) { | ||
this.autoRender = opts.autoRender; | ||
// &-view requires this.template && this.autoRender to be truthy in | ||
// order to autoRender. template doesn't apply to &-form-view, but | ||
// we manually flip the bit to honor autoRender | ||
this.template = opts.template || this.template || true; | ||
} | ||
if (opts.autoRender) this.autoRender = opts.autoRender; | ||
|
||
this.template = opts.template || this.template; | ||
|
||
if (opts.values) this._startingValues = opts.values; | ||
|
||
|
@@ -124,7 +126,7 @@ module.exports = View.extend({ | |
}, | ||
|
||
remove: function () { | ||
this.el.removeEventListener('submit', this.handleSubmit, false); | ||
this.formEl.removeEventListener('submit', this.handleSubmit, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would throw an error if there's ever a case that |
||
this._fieldViewsArray.forEach(function (field) { | ||
field.remove(); | ||
}); | ||
|
@@ -164,11 +166,13 @@ module.exports = View.extend({ | |
|
||
render: function () { | ||
if (this.rendered) return; | ||
if (!this.el) { | ||
this.el = document.createElement('form'); | ||
} | ||
|
||
this.renderWithTemplate(this); | ||
|
||
this.formEl = this.query('form') || this.el; | ||
|
||
if (this.autoAppend) { | ||
this.fieldContainerEl = this.el.querySelector('[data-hook~=field-container]') || this.el; | ||
this.fieldContainerEl = this.queryByHook('field-container') || this.formEl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took the liberty of using this ampersand view convenience method ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
} | ||
this._fieldViewsArray.forEach(function renderEachField(fV) { | ||
this.renderField(fV, true); | ||
|
@@ -183,7 +187,7 @@ module.exports = View.extend({ | |
delete this._startingValues; | ||
} | ||
this.handleSubmit = this.handleSubmit.bind(this); | ||
this.el.addEventListener('submit', this.handleSubmit, false); | ||
this.formEl.addEventListener('submit', this.handleSubmit, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have bias to retaining There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason this is necessary is in cases where the root When you use your form view in your app, you shouldn't ever need to access There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. understood.
is this case a reality, however? FormView is essentially a wrapper around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, in my personal case it is: I want to wrap this view in a But rather than pushing for my personal use-case, I'd suggest that it's more so about allowing the view's
Another way of thinking of it would be to use: this.query('form').addEventListener('submit', this.handleSubmit, false);
this.query('form').removeEventListener('submit', this.handleSubmit, false); Using |
||
// force `change:valid` to be triggered when `valid === false` post-render, | ||
// despite `valid` not having changed from its default pre-render value of `false` | ||
this.set('valid', null, {silent: true}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used a fieldset, which from what I understand is the most appropriate field container element. Technically you could do without this line and put the
data-hook
in the<form>
(or even leave the hook out because it defaults to the<form>
element if no hook is found). And of course this can be overridden when extending the form view or instantiating it (with an option).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not opposed to a fieldset. i would consider this a breaking change however, as any DOM node type or hierarchy manipulations will impact folks' CSS rules. it may also be a nice addition for some people to have more immediate structure. the counter to this is that it's not strictly necessary/bare-minimum, and is easily overridden with a template
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, particularly the last one. In that case, I can update it to remove the
<fieldset>
tag and putdata-hook="field-container"
in the<form>
tag.