typescript enum default value -
i starting learn typescript aurelia framework. have implemented matthew davis's blog typescript enums in aurelia templates using viewenginehooks http://davismj.me/blog/template-constants/ aurelia's todo app.
i please set default enum value 2nd value in list , setting default 1st value in list.
also please know if {todo, priority} or {todo} needs importing shown in todo-list.ts
todo.ts
// pro tip: starting our enum @ 1, ensure values in enum truthy. export enum priority { high = 1, medium, low } export class todo { @observable done; //*** setting priority: priority = 2 or priority: priority = priority.medium - not change default 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 - not change default high / 1 ; //this.priority = 2; this.priority = priority.medium; this.editing = false; }
todo-list.ts
//*** {todo} or {todo, priority} needed importing? *** //import {todo} './todo'; import {todo, priority} './todo'; ... add(description) { if (description) { //*** setting (this, description, 2) or (this, description, priority.medium) - not change default 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 know when declaring variable can't set default value of enum, same way number or boolean hasn't default value. however, can define default value function parameter, did in constructor (priority: priority = priority.medium
), don't have supply parameter when calling constructor.
two additional notes: shouldn't this.priority = priority; this.editing = editing;
instead of this.priority = priority.medium; this.editing = false;
? , second, if put public
in front of parameters class properties automatically added , assigned, therefore don't need additional lines of constructor. more complex classes create , assign properties manually.
concerning second question: need import priority
reference enum, example when writing priority.medium
. don't have import it, when compare example 2 different properties of type priority
without using enum's name (for example this.todos[0].priority === this.todos[1].priority
).
Comments
Post a Comment