commit e76e49aa61b3e197c6f08694bca28aa658d85517 Author: Märt Kalmo Date: Wed May 17 18:36:09 2017 +0300 starting state diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd185fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +node_modules + + +/build +/.settings +/.classpath +/.project + +/.idea +*.iml +*.log diff --git a/app/add.ctrl.ts b/app/add.ctrl.ts new file mode 100644 index 0000000..779fdb1 --- /dev/null +++ b/app/add.ctrl.ts @@ -0,0 +1,24 @@ +import { Post } from './post.cls'; +import { DataService } from './data.srv.i'; +import { ILocationService } from 'angular'; + +function AddCtrl($location: any, dataService: any) { + + var vm = this; + vm.title = ''; + vm.text = ''; + vm.addNew = addNew; + + function addNew() { + var post = { title: vm.title, text: vm.text }; // use new Post(.. instead + + dataService.addPost(post) + .then(function () { + $location.path('/list'); + }); + } +} + +AddCtrl.$inject = ['$location', 'dataService']; + +angular.module('app').controller('AddCtrl', AddCtrl); diff --git a/app/add.html b/app/add.html new file mode 100644 index 0000000..4a24095 --- /dev/null +++ b/app/add.html @@ -0,0 +1,5 @@ +Pealkiri:
+ +Text:
+ + diff --git a/app/angular.d.ts b/app/angular.d.ts new file mode 100644 index 0000000..cf40e65 --- /dev/null +++ b/app/angular.d.ts @@ -0,0 +1,4 @@ +declare global { + const angular: ng.IAngularStatic; +} +export {}; diff --git a/app/app.ts b/app/app.ts new file mode 100644 index 0000000..cb7298a --- /dev/null +++ b/app/app.ts @@ -0,0 +1,13 @@ +/// + +var app = angular.module('app', ['ngRoute']); + +import './mem.data.srv'; +import './add.ctrl'; +import './list.ctrl'; +import './route.conf'; + +angular.element(document) + .ready(function () { + angular.bootstrap(document, ['app']); + }); diff --git a/app/data.srv.i.ts b/app/data.srv.i.ts new file mode 100644 index 0000000..abc56d9 --- /dev/null +++ b/app/data.srv.i.ts @@ -0,0 +1,8 @@ +import { Post } from './post.cls'; + +export interface DataService { + + addPost(post: Post): Promise; + + getPosts(): Promise; +} diff --git a/app/list.ctrl.ts b/app/list.ctrl.ts new file mode 100644 index 0000000..20eb5a0 --- /dev/null +++ b/app/list.ctrl.ts @@ -0,0 +1,21 @@ +import { Post } from './post.cls'; +import { DataService } from "./data.srv.i"; + +function ListCtrl(dataService: any) { + + var vm = this; + vm.posts = []; + + init(); + + function init() { + dataService.getPosts() + .then(function (result : any) { + vm.posts = result; + }); + } +} + +ListCtrl.$inject = ['dataService']; + +angular.module('app').controller('ListCtrl', ListCtrl); diff --git a/app/list.html b/app/list.html new file mode 100644 index 0000000..de50c8c --- /dev/null +++ b/app/list.html @@ -0,0 +1,3 @@ +
+ {{ post.title }} +
diff --git a/app/mem.data.srv.ts b/app/mem.data.srv.ts new file mode 100644 index 0000000..b07f50d --- /dev/null +++ b/app/mem.data.srv.ts @@ -0,0 +1,29 @@ +import { Post } from './post.cls'; +import { DataService } from './data.srv.i'; +import { IQService } from 'angular'; + +function MemDataService($q: any) { + + var vm = this; + vm.addPost = addPost; + vm.getPosts = getPosts; + + vm.posts = [ + new Post('Post 1', 'text 1'), + new Post('Post 2', 'text 2') + ]; + + function addPost(post: Post) { + vm.posts.push(post); + + return $q.resolve(); + } + + function getPosts() { + return $q.resolve(vm.posts); + } +} + +MemDataService.$inject = ['$q']; + +angular.module('app').service('dataService', MemDataService); diff --git a/app/post.cls.ts b/app/post.cls.ts new file mode 100644 index 0000000..ac88c66 --- /dev/null +++ b/app/post.cls.ts @@ -0,0 +1,3 @@ +export class Post { + constructor(private title: string, private text: string) {} +} diff --git a/app/rest.data.srv.ts b/app/rest.data.srv.ts new file mode 100644 index 0000000..50fe128 --- /dev/null +++ b/app/rest.data.srv.ts @@ -0,0 +1,22 @@ +import { Post } from './post.cls'; +import { DataService } from './data.srv.i'; +import { IHttpService } from 'angular'; + +export class RestDataService implements DataService { + + constructor(private $http: IHttpService) {} + + addPost(post: Post) { + return this.$http.post('api/posts', post) + .then(() => null); + } + + getPosts() { + return this.$http.get('api/posts') + .then(result => result.data); + } +} + +RestDataService.$inject = ['$http']; + +angular.module('app').service('dataService', RestDataService); diff --git a/app/route.conf.ts b/app/route.conf.ts new file mode 100644 index 0000000..72d2aeb --- /dev/null +++ b/app/route.conf.ts @@ -0,0 +1,21 @@ +import { ILocationProvider } from 'angular'; + +routeConf.$inject = ['$routeProvider', '$locationProvider']; + +function routeConf($routeProvider: any, + $locationProvider: any) { + + $routeProvider.when('/list', { + templateUrl : 'app/list.html', + controller : 'ListCtrl', + controllerAs : 'vm' + }).when('/new', { + templateUrl : 'app/add.html', + controller : 'AddCtrl', + controllerAs : 'vm' + }).otherwise('/list'); + + $locationProvider.hashPrefix(''); +} + +angular.module('app').config(routeConf); diff --git a/data/db.json b/data/db.json new file mode 100644 index 0000000..8bdb784 --- /dev/null +++ b/data/db.json @@ -0,0 +1,19 @@ +{ + "posts": [ + { + "title": "Post 1", + "text": "text 1", + "_id": 1 + }, + { + "title": "Post 2", + "text": "text 2", + "_id": 2 + }, + { + "title": "a", + "text": "b", + "_id": 3 + } + ] +} \ No newline at end of file diff --git a/data/routes.json b/data/routes.json new file mode 100644 index 0000000..c042bf3 --- /dev/null +++ b/data/routes.json @@ -0,0 +1,3 @@ +{ + "/api/": "/" +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..6ee6b14 --- /dev/null +++ b/index.html @@ -0,0 +1,18 @@ + + + + + + + + + + + Kõik postitused | Lisa uus + +


+ +
+ + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..c3e46dc --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "ng1ts", + "version": "1.0.0", + "scripts": { + "serve": "webpack-dev-server --port 3001", + "build-sample": "webpack --config webpack.sample.config.js", + "build": "webpack", + "back-end": "json-server ./data/db.json --static ./ --id _id --port 3000 --routes ./data/routes.json" + }, + "devDependencies": { + "@types/angular": "^1.6.15", + "@types/angular-route": "^1.3.3", + "@types/es6-promise": "0.0.32", + "angular": "^1.6.4", + "angular-route": "^1.6.4", + "awesome-typescript-loader": "^3.1.2", + "json-server": "^0.8.22", + "typescript": "^2.2.2", + "webpack": "^2.5.1", + "webpack-dev-server": "^2.4.5" + } +} diff --git a/post.ts b/post.ts new file mode 100644 index 0000000..299871c --- /dev/null +++ b/post.ts @@ -0,0 +1,3 @@ +export class Post { + constructor(public title: string, public text: string) {} +} diff --git a/sample.ts b/sample.ts new file mode 100644 index 0000000..11713d1 --- /dev/null +++ b/sample.ts @@ -0,0 +1,23 @@ +import { Post } from './post'; + +class Dao { + getPost() { + let dataFromServer = '{ "title": "Post 1", "text": "text 1" }'; + + return JSON.parse(dataFromServer); + } + + getRealPost() { + return new Post('Title 1', 'text 1'); + } + + getPostTyped(): Post { + let dataFromServer = '{ "title": "Post 1", "text": "text 1" }'; + + return JSON.parse(dataFromServer); + } +} + +let dao = new Dao(); + +console.log(dao.getRealPost()); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..0791944 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "types" : ["angular", "angular-route", "es6-promise"], + "target": "es5", + "moduleResolution": "node", + "sourceMap": false, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "noImplicitAny": true, + "allowJs": true, + "suppressImplicitAnyIndexErrors": true + } +} \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..b7766b1 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,29 @@ +var path = require('path'); + +module.exports = { + + entry: { + app: './app/app.ts' + }, + + output: { + path: path.resolve(__dirname, 'build'), + publicPath: '/build/', + filename: 'bundle.js' + }, + + module: { + loaders: [ + { + test: /\.ts$/, + loader: 'awesome-typescript-loader' + } + ], + exprContextCritical: false + }, + + resolve: { + extensions: ['.js', '.ts'] + } + +}; diff --git a/webpack.sample.config.js b/webpack.sample.config.js new file mode 100644 index 0000000..4e4281a --- /dev/null +++ b/webpack.sample.config.js @@ -0,0 +1,5 @@ +var conf = require('./webpack.config.js'); + +conf.entry.app = './sample.ts'; + +module.exports = conf;