starting state

This commit is contained in:
Märt Kalmo
2017-05-17 18:36:09 +03:00
commit e76e49aa61
21 changed files with 300 additions and 0 deletions

24
app/add.ctrl.ts Normal file
View File

@@ -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);

5
app/add.html Normal file
View File

@@ -0,0 +1,5 @@
Pealkiri: <input ng-model="vm.title"> <br>
Text: <input ng-model="vm.text"><br>
<button ng-click="vm.addNew()">Saada</button>

4
app/angular.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
declare global {
const angular: ng.IAngularStatic;
}
export {};

13
app/app.ts Normal file
View File

@@ -0,0 +1,13 @@
/// <reference path="angular.d.ts" />
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']);
});

8
app/data.srv.i.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Post } from './post.cls';
export interface DataService {
addPost(post: Post): Promise<void>;
getPosts(): Promise<Post[]>;
}

21
app/list.ctrl.ts Normal file
View File

@@ -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);

3
app/list.html Normal file
View File

@@ -0,0 +1,3 @@
<div ng-repeat="post in vm.posts">
{{ post.title }}
</div>

29
app/mem.data.srv.ts Normal file
View File

@@ -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);

3
app/post.cls.ts Normal file
View File

@@ -0,0 +1,3 @@
export class Post {
constructor(private title: string, private text: string) {}
}

22
app/rest.data.srv.ts Normal file
View File

@@ -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(() => <void> null);
}
getPosts() {
return this.$http.get('api/posts')
.then(result => result.data);
}
}
RestDataService.$inject = ['$http'];
angular.module('app').service('dataService', RestDataService);

21
app/route.conf.ts Normal file
View File

@@ -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);