I am just starting to learn Typescript with the Aurelia framework. I have implemented Matthew Davis's blog TypeScript Enums in Aurelia Templates Using ViewEngineHooks http://davismj.me/blog/template-constants/ to the Aurelia's Todo app.
I would please like to set the default enum value to the 2nd value in the list and is setting the default to the 1st value in the list.
Also I would please like to know if {Todo, Priority} or just {Todo} needs importing as shown in the todo-list.ts
todo.ts
// Pro Tip: By starting our enum at 1, we ensure that all values in the enum are truthy.
export enum Priority {
High = 1,
Medium,
Low
}
export class Todo {
@observable done;
//*** Setting priority: Priority = 2 OR priority: Priority = Priority.Medium - Does not change the default from High / 1 ***
//constructor(public list: TodoList, public description: string, public priority: Priority = 2, public editing: boolean = false) {
constructor(public list: TodoList, public description: string, public priority: Priority = Priority.Medium, public editing: boolean = false) {
this.list = list;
this.description = description;
//*** Setting this.priority = 2 OR this.priority = Priority.Medium - Does not change the default from High / 1 ;
//this.priority = 2;
this.priority = Priority.Medium;
this.editing = false;
}
todo-list.ts
//*** Is {Todo} OR {Todo, Priority} needed for importing? ***
//import {Todo} from './todo';
import {Todo, Priority} from './todo';
...
add(description) {
if (description) {
//*** Setting (this, description, 2) OR (this, description, Priority.Medium) - Does not change the default from High / 1 ***
//this.todos.push(new Todo(this, description, 2));
this.todos.push(new Todo(this, description, Priority.Medium));
this.invalidateView();
}
}
todo.html
<select id="priority" value.bind="type">
<option value.bind="Priority[type]" repeat.for="type of Prioritys">${type}</option>
</select>
As far as I know when declaring a variable you can't set the default value of an enum, the same way as a number or boolean hasn't a default value.
However, you can define a default value for a function parameter, as you did in the constructor (priority: Priority = Priority.Medium
), so that you don't have to supply that parameter when calling the constructor.
Two additional notes:
Shouldn't it be this.priority = priority; this.editing = editing;
instead of this.priority = Priority.Medium; this.editing = false;
? And second, if you put public
in front of the parameters the class properties are automatically added and also assigned, you therefore don't need the additional lines of your constructor. However for more complex classes I would probably create and assign the properties manually.
Concerning your second question: You need to import Priority
as soon as you reference that enum, for example when writing Priority.Medium
. You don't have to import it, when you compare for example two different properties of type Priority
without using the enum's name (for example this.todos[0].priority === this.todos[1].priority
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With