Firstly, we need to add some support libs with script tags. you can find them in /libs sub directory of tinyts or their offical website, which is mentioned in tinyts readme.
import { AncView, v } from'tinyts/core/tinyts';import { ListView } from'tinyts/control/list';import { InputView } from"tinyts/control/input";enumKeyCode { ENTER =13}classTodoData {constructor(public Name:string) { }}classTodoDemoextendsAncView { data:TodoData[]; @v(ListView) list:ListView<TodoData>; @v(InputView) newItem:InputView;AfterInject() {this.newItem.On("keydown", (ev:JQueryKeyEventObject) => {if (ev.keyCode ==KeyCode.ENTER) {this.data.push(newTodoData(this.newItem.Value()));this.newItem.SetValue(""); } });this.list.SetEventHandler("li", (obj) => {var text =$(obj.target).text();for (var i =0; i <this.data.length; i++) {if (this.data[i].Name == text) {this.data.splice(i,1); } } });this.data = [newTodoData("First"),newTodoData("Second")]; }}// create a TodoDemo instance.var aa =newTodoDemo();
TodoDemo is the context in this example. some of its properties are bound with DOM element. That means what we do to TodoDemo's listwill effect to ul#list element.
Notice the @v decorator upon the list property, it'll establish the connection to the html element via id attribute, here the dependency injection feature works.
The data-bind attribute tells tinyts that the DOM ul element is bound with data property in class TodoDemo.
And the AfterInject method will be triggered after all injection finish(it a hook feature).