-
Notifications
You must be signed in to change notification settings - Fork 16
fixed and improved JS scaffolding #32
base: develop
Are you sure you want to change the base?
Changes from all commits
32ad302
d8ccf82
04d65e9
16a5113
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 |
|---|---|---|
| @@ -1,59 +1,58 @@ | ||
| /*! | ||
| import EstaticoModule from '../../assets/js/helpers/module'; | ||
|
|
||
| /* | ||
| * {{originalName}} | ||
| * | ||
| * @author | ||
| * @copyright | ||
| */ | ||
| import EstaticoModule from '../../assets/js/helpers/module'; | ||
|
|
||
| class {{className}} extends EstaticoModule { | ||
| export default class {{className}} extends EstaticoModule { | ||
| static events = { | ||
|
Collaborator
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. While I certainly like the idea, could a stage 2 proposal be a bit too experimental to be used in production already? https://github.com/tc39/proposal-class-public-fields Judging from https://github.com/babel/babel/search?q=static+property&type=Issues&utf8=%E2%9C%93, there might still be some cases where the transpiled result might not be the expected one.
Contributor
Author
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. Did not see any major problems there, most problems are caused by wrong usage - e.g. some guys did not understand that a class member belongs to a class and not to the instance and of course the inheritance works different.
Contributor
Author
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. (Even if not using this version, it's still better to go back to constants defined outside the class. At the moment the objects are created over and over again for every instance.) |
||
| // eventname: `eventname.estatico.${{{className}}.name}` | ||
| }; | ||
| static defaultData = {}; | ||
| static defaultOptions = { | ||
| domSelectors: { | ||
| // item: `[data-${{{className}}.name}}="item"]` | ||
| }, | ||
| stateClasses: { | ||
| // activated: 'is_activated' | ||
| } | ||
| }; | ||
|
|
||
| constructor($element, data, options) { | ||
| let _defaultData = {}, | ||
| _name = '{{name}}', | ||
| _defaultOptions = { | ||
| domSelectors: { | ||
| // item: '[data-' + _name + '="item"]' | ||
| }, | ||
| stateClasses: { | ||
| // activated: 'is_activated' | ||
| } | ||
| }; | ||
|
|
||
| super($element, _defaultData, _defaultOptions, data, options); | ||
| super($element, {{className}}.defaultData, {{className}}.defaultOptions, data, options); | ||
|
|
||
| this._initUi(); | ||
| this._initEventListeners(); | ||
| } | ||
|
|
||
| static get events() { | ||
| return { | ||
| // eventname: 'eventname.estatico.' + {{name}} | ||
| }; | ||
| /** | ||
| * Unbind events, remove data, custom teardown | ||
| * | ||
| * @public | ||
| */ | ||
| destroy() { | ||
| super.destroy(); | ||
|
|
||
| // Custom destroy actions go here | ||
| } | ||
|
|
||
| /** | ||
| * Initialisation of variables, which point to DOM elements | ||
| * | ||
| * @private | ||
| */ | ||
| _initUi() { | ||
| // DOM element pointers | ||
| } | ||
|
|
||
| /** | ||
| * Event listeners initialisation | ||
| * | ||
| * @private | ||
| */ | ||
| _initEventListeners() { | ||
| // Event listeners | ||
| } | ||
|
|
||
| /** | ||
| * Unbind events, remove data, custom teardown | ||
| */ | ||
| destroy() { | ||
| super.destroy(); | ||
|
|
||
| // Custom destroy actions go here | ||
| } | ||
| } | ||
|
|
||
| export default {{className}}; | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
We might consider forcing the lowercase again. We usually use
this.namefor data-attributes matching and - quoting specs - these have to be lowercase.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.
No reason to convert the case to lower case. What will happen, you can see in the current master branch - sometimes everything breaks, because everyone is using a different case. (Yes, the master branch is broken at the moment.)
Also it is not possible to convert this outside an instance, e.g. getting a class' name in the right case. It only works after an instance is created and needs to be converted for each instance over and over agin. Please also see #33 where I found a quite clean solution for the module registration.
(In our current setup we convert original class names like "Image Gallery" to the file name "image_gallery" and class name "ImageGallery". By using the class name instead of an extra lowercase for the assignment, we don't need a third case.)
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.
@swey, so you would use
[data-' + this.name.toLowerCase() + '="bla"']and$domElement.data(this.name.toLowerCase())instead?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.
Ah, got it. Sorry, I only thought of
data-init. (We use a different data binding, so I forgot about that.)Don't have a good solution in mind right now. Will think about it
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 definitely like the approach from #32 and #33. However the
data-attributes management remains open, and the approach from @backflip ([data-' + this.name.toLowerCase() + '="bla"']) seems rather error prone. How about havingthis.nameandthis._name/this.dataNamefor these 2 purposes (I know, my naming skills are really bad… feel free to propose another one).This has moreover impact on linting configuration and how we would have to specify defaults in
head.js: we would need to use PascalCased object properties instead of "camelCased". Note that many timeshead.jsis used as the configuration point for backend developers, so having proper naming conventions there is important. @backflip would it be fine for you to change linting rules to accept as well PascalCased object properties?Uh oh!
There was an error while loading. Please reload this page.
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.
The main problem is, that working with
this.nameis only working inside an instance. Which means that all properties must be set at runtime and for every instance again (which can cause a lot of extra operations and will block more memory). But well, of course we're only talking of quite simple and cheap string operations.But I don't see a clean solution for your data binding besides adding a
const name = 'module_in_yor_prefered_case';before the class definition - very similar to the old version of estatico.(In our team we use
data-js-bindingwithout the module name. But of course binding to multiple instances is not that clean. On the other hand it solved a lot of problems with nested modules for us.)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.
Having a new idea on this. Coming back with it the next days :)