dojo / cli-build-webpack

🚀 **DEPRECATED** Dojo 2 - cli command for building applications
http://dojo.io
Other
4 stars 19 forks source link

Question: Union Types in properties #164

Closed sebilasse closed 7 years ago

sebilasse commented 7 years ago

Sometimes I have union types (or enum) in properties. e.g. in Grid I am using an enum so the columns does not need to be 'eleven' but can be 11. But since 'eleven' is a CSS class, my widget would always need to use the string 'eleven'. What is the most easy way to "cast" this.properties (do not know exact engl. term) ?

kitsonk commented 7 years ago

This seems like a general TypeScript issue, versus anything to do with Dojo 2 or anything to do with the build tooling.

sebilasse commented 7 years ago

@kitsonk Thanks for quick answers ! Let me explain : this.properties in any w is "read-only", right ?

I am basically looking for a way to change this.properties prior to render ... It would be nice to have a method like beforeRender but just to change properties (?)

kitsonk commented 7 years ago

I am unsure how this relates to the build tool.

Can you provide an example of what is not working? You seem to be implying that you want different variable names in a code block. There are many different ways to change the local name of variables in a code block, including when destructering. Again, none of these are specific to Dojo 2 or even TypeScript. There are basic JavaScript concepts. But with just discussing abstract concepts without any example code, it is really hard to deal with.

agubler commented 7 years ago

@sebilasse you can change the properties on a beforeRender they get passed as the original reference as the second argument.

function beforeRender(renderFunction: () => DNode | DNode[], properties: any, children: any) {
     properties.changedMe = false;
     return renderFunction;
}

However I would ask whether or not actually changing the property value is the correct answer to your problem...

sebilasse commented 7 years ago

@kitsonk Argh, sorry. This issue should be in widget-core ... The question is simply : Why is this.properties 'read only' and why can't I convert / change properties of it but some second before I write this word @agubler answered what I expected.

However I would ask whether or not actually changing the property value is the correct answer to your problem...

It is used very rare (where it makes sense, e.g. in grid). Justification: It is still because the Semantic UI dojo integration should be fully compatible with the official React one (posted preview code with bugs in https://github.com/dojo/widget-core/issues/559) ;) Btw: The main bug in the code is that the Base Class had a Typo in its generic typings where it should extend P (the generic), not Properties (the default)...

agubler commented 7 years ago

@sebilasse it's a core principle of dojo 2 that properties should not be changed in the render. Widgets should be considered pure in terms of their properties. That is why they are readonly.

kitsonk commented 7 years ago

The property itself is strictly read only, in that it is a getter of a shadowed variable. But as @agubler says, it is also anti-pattern to change the values of any of the properties of properties within the widget, not only the desire for pure methods within the widget, but also because it becomes difficult to offer up the APIs that allow you to introspect changes to properties which is provided by widget-core. If you need to do some calculation/adjustment/interpretation of a property in a render function it is a lot better assign that value to a local variable within the render function.

kitsonk commented 7 years ago

Issue moved to dojo/widget-core #587 via ZenHub