Yaniv Efraim
Wix.com
I am a Javascript engineer @Wix
Passionate about Javascript and Angular. Hacking on migrating to Angular 2.0 stuff on the last few months.
(Write everything from scratch)
import {Component, View, EventEmitter} from 'angular2/core'; @Component({ selector: 'font-size-component', inputs: ['fontSize'], outputs: ['fontSizeChange'] }) @View({ template: `<input [ng-model]="fontSize" id="fontSize">`, directives: [FORM_DIRECTIVES] }) export class FontSizeComponent { fontSize: string; fontSizeChange: EventEmitter = new EventEmitter(); modelChanged($event) { this.fontSizeChange.emit($event); } }
<div ng-controller="MainCtrl as mainCtrl"> <h1>Items</h1> <ul> <li ng-repeat="item in mainCtrl.items"> {{item.text}} <button ng-click="mainCtrl.deleteItem(item)"> Delete </button> </li> </ul> </div>
function MainController() { var ctrl = this; ctrl.items = [{title: 'title 1', text: 'item 1'}, {...}]; ctrl.deleteItem = function(item) { var idx = ctrl.items.indexOf(item); if (idx >= 0) { ctrl.items.splice(idx, 1); } }; }
<div ng-controller="MainCtrl as mainCtrl"> <h1>Items</h1> <!-- <ul> <li ng-repeat="item in mainCtrl.items"> { {item.text}} <button ng-click="mainCtrl.deleteItem(item)"> Delete </button> </li> </ul> --> <item-list items="mainCtrl.items"></item-list> </div>
module.directive('itemList', function() { return { scope: {}, controller: function() { var ctrl = this; ctrl.deleteItem = function(item) { var idx = ctrl.items.indexOf(item); if (idx >= 0) { ctrl.items.splice(idx, 1); } }; }, controllerAs: 'itemListCtrl', bindToController: { items: '=' }, templateUrl: 'item-list.html' }; });
(our component is mutating data it doesn't own!)
ctrl.deleteItem = function(item) { var idx = ctrl.items.indexOf(item); if (idx >= 0) { ctrl.items.splice(idx, 1); } };
module.directive('itemList', function() { return { scope: {}, controller: function() { var ctrl = this; ctrl.deleteItem = function(item) { ctrl.onDelete({item: item}); }; }, controllerAs: 'itemListCtrl', bindToController: { items: '=', onDelete: '&' }, templateUrl: 'item-list.html' }; });
<div ng-controller="MainCtrl as mainCtrl"> <h1>Items</h1> <item-list items="mainCtrl.items" on-delete="mainCtrl.onDelete(item)"></item-list> </div>
module.directive('itemList', function() { return { scope: {}, controller: function() { var ctrl = this; ctrl.deleteItem = function(item) { ctrl.onDelete({item: item}); }; }, controllerAs: 'itemListCtrl', bindToController: { items: '=', //input onDelete: '&' //output }, templateUrl: 'item-list.html' }; });
<div ng-controller="MainCtrl as ctrl"> <div class="left-sidebar"> <select class="form-control" ng-model="ctrl.fontFamily" ng-options="font for font in ctrl.fonts"></select> <input id="fontColor" ng-model="ctrl.fontColor" placeholder="#nnnnnn or <color-name>"> <input ng-model="ctrl.fontSize"> </div> <div class="main panel panel-primary"> <div ng-style="{'font-family': ctrl.fontFamily, 'color': ctrl.fontColor, 'font-size': ctrl.fontSize}"> <p>Lorem ipsum dolor sit amet, consectetur ...</p> </div> </div> </div>
<div class="left-sidebar"> <theme-editor-panel-component font-size="{{ctrl.fontSize}}" font-family="{{ctrl.fontFamily}}" font-color="{{ctrl.fontColor}}" on-font-size-change="ctrl.onFontSizeChange(fontSize)" on-font-color-change="ctrl.onFontColorChange(fontColor)" on-font-family-change="ctrl.onFontFamilyChange(fontFamily)"></theme-editor-panel-component> </div> <div class="main panel panel-primary"> <theme-preview-panel-component font-size="{{ctrl.fontSize}}" font-family="{{ctrl.fontFamily}}" font-color="{{ctrl.fontColor}}"></theme-preview-panel-component> </div>
<div class="control-group"> <label class="control-label">Font Family</label> <font-family-component font-family="{{ctrl.fontFamily}}" font-family-changed="ctrl.onFontFamilyChangeEvent(fontFamily)"></font-family-component> </div> <div class="control-group"> <label class="control-label" for="fontColor">Font Color</label> <font-color-component font-color="{{ctrl.fontColor}}" font-color-changed="ctrl.onFontColorChangeEvent(fontColor)"></font-color-component> </div> <div class="control-group"> <label class="control-label" for="fontSize">Font Size</label> <font-size-component font-size="{{ctrl.fontSize}}" font-size-changed="ctrl.onFontSizeChangeEvent(fontSize)"></font-size-component> </div>
directive('fontSizeComponent', function() { return { template: '<input ng-model="ctrl.fontSize" ng-change="ctrl.modelChanged()">', scope: {}, bindToController: { fontSize: "@", fontSizeChanged: "&" }, controllerAs: 'ctrl', controller: function() { var that = this; this.modelChanged = function() { that.fontSizeChanged({fontSize: that.fontSize}); }; } }; });
//TypeScript var bar: string; var func: Function;
//Compiled to var bar; var func;
import angular from 'angular'; class FontSizeComponent { /* @ngInject */ constructor() { } modelChanged() { this.fontSizeChanged({fontSize: this.fontSize}); } } export default angular.module('themeCreatorFontSizeComponentModule', []) .directive('fontSizeComponent', function() { return { template: `<input ng-model="ctrl.fontSize" ng-change="ctrl.modelChanged()">`, scope: { fontSize: "@", fontSizeChanged: "&" }, bindToController: true, controllerAs: 'ctrl', controller: FontSizeComponent }; });
import angular from 'angular'; class FontSizeComponent { fontSize: string; //font size is of type string fontSizeChanged: Function; //fontSizeChanged is of type Function /* @ngInject */ constructor() { } modelChanged() { this.fontSizeChanged({fontSize: this.fontSize}); } } export default angular.module('themeCreatorFontSizeComponentModule', []) .directive('fontSizeComponent', function() { return { template: `<input ng-model="ctrl.fontSize" ng-change="ctrl.modelChanged()">`, scope: { fontSize: "@", fontSizeChanged: "&" }, bindToController: true, controllerAs: 'ctrl', controller: FontSizeComponent }; });
import {Component, View, EventEmitter} from 'angular2/core'; @Component({ selector: 'font-size-component', inputs: ['fontSize'], outputs: ['fontSizeChange'] }) @View({ template: `<input [ngmodel]="fontSize" (ngmodelchanged)="modelChanged($event)" id="fontSize">` }) export class FontSizeComponent { fontSize: string; fontSizeChange: EventEmitter = new EventEmitter(); modelChanged($event) { this.fontSizeChange.emit($event); } }
app.controller('AppController', function ($router) { $router.config([ { path: '/welcome', component: 'welcome' } ]); });