-
Notifications
You must be signed in to change notification settings - Fork 40
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
Define Widgets and Tags via Classes as an option #91
Comments
Hi @kristianmandrup, that is kind of supported, but the problem is that the
Those static methods won't get added if you just export a class. Instead, you would need to do the following: export default class Widget {
constructor(widgetConfig) {
}
}
var renderer = require('marko-widgets').defineRenderer({
template: require('./template.marko'),
getTemplateData: function(state, input) {
// ...
}
});
Widget.renderer = Widget.prototype.renderer = renderer;
Widget.render = renderer.render; While it would be nice to just use the |
The elegant solutions: ES7 decorators ;) @Widget({template: '../cool/template.marko'})
export default class MyCoolWidget extends CoolWidget {
constructor(widgetConfig) {
super(widgetConfig);
}
} https://github.com/wycats/javascript-decorators It is also possible to decorate the class itself. In this case, the decorator takes the target constructor. http://www.2ality.com/2015/01/es6-destructuring.html Destructuring can be used to effectively "simulate" optional function arguments // Widget decorator: adds static renderer function to class
function Widget({template = './template.marko', templateData = undefined} = {}) {
return function decorator(constructor)
var templateData = templateData || constructor.prototype.getTemplateData;
var renderer = require('marko-widgets').defineRenderer({
template: require(template), // "configuration then convention " ;)
getTemplateData: function(state, input) {
templateData(state, input);
}
});
constructor.renderer = constructor.prototype.renderer = renderer;
constructor.render = renderer.render;
}
} Pretty cool I should say :) |
Hey, I'm trying a small experimental project using ES7 with marko templates. https://github.com/kristianmandrup/marko-es7 However when I try your proposed pattern using
I get an error: SyntaxError: /Users/kristianmandrup/repos/test123/marko-es7/dist/template.marko: Unexpected token (1:6)
> 1 | Hello $data.name!
| ^
at Parser.pp.raise (/Users/kristianmandrup/repos/test123/marko-es7/node_modules/babel-core/node_modules/babylon/lib/parser/location.js:24:13) Looks like the babel parser is trying to parse it. Should just be "required" by the regular node Thanks ;) |
I kinda have it working now, except I'm a bit lost on how to test this infrastructure properly :P Test template config use of template How do I access and use the template correctly here? |
Unless I am missing something, the user should not be implementing the Might be premature to use ES7, but interesting none the less. Personally, I am wary of decorators since I think they were abused in languages like Java (i.e. Java annotations). |
Hey @kristianmandrup, were you able to come up with a good solution based on ES7 decorators? |
Since I don't know the internals of |
Now that Node.js 4.0 is out and most other modern frameworks leverage the concept of Classes in Javascript, I think it would be very appropriate if Marko Widgets (and Marko Tags in general) supported class definitions as a (modern) alternative. You could even leverage decorators, for those using Babel.
Could be defined as a class, like this.
Using class properties
or pure ES2015 getter
Looks pretty cool and concise to me :) The nice thing is that it would be easy to do widget inheritance as well. Decorators can be used to provide mixin like behavior if needed (like in React components).
The text was updated successfully, but these errors were encountered: