واکشی اطلاعات سرویس Web Api با استفاده از TypeScript و AngularJs
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۲/۱۰/۰۹ ۸:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public abstract class Entity
{
public Guid Id { get; set; }
} public class Book : EntityBase
{
public string Name { get; set; }
public decimal Author { get; set; }
} public class BookRepository
{
private readonly ConcurrentDictionary<Guid, Book> result = new ConcurrentDictionary<Guid, Book>();
public IQueryable<Book> GetAll()
{
return result.Values.AsQueryable();
}
public Book Add(Book entity)
{
if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid();
if (result.ContainsKey(entity.Id)) return null;
if (!result.TryAdd(entity.Id, entity)) return null;
return entity;
}
} public class BooksController : ApiController
{
public static BookRepository repository = new BookRepository();
public BooksController()
{
repository.Add(new Book
{
Id=Guid.NewGuid(),
Name="C#",
Author="Masoud Pakdel"
});
repository.Add(new Book
{
Id = Guid.NewGuid(),
Name = "F#",
Author = "Masoud Pakdel"
});
repository.Add(new Book
{
Id = Guid.NewGuid(),
Name = "TypeScript",
Author = "Masoud Pakdel"
});
}
public IEnumerable<Book> Get()
{
return repository.GetAll().ToArray();
}
} module Model {
export class Book{
Id: string;
Name: string;
Author: string;
}
} <div ng-controller="Books.Controller">
<table class="table table-striped table-hover" style="width: 500px;">
<thead>
<tr>
<th>Name</th>
<th>Author</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="book in books">
<td>{{book.Name}}</td>
<td>{{book.Author}}</td>
</tr>
</tbody>
</table>
</div> declare module AngularModule {
export interface HttpPromise {
success(callback: Function) : HttpPromise;
}
export interface Http {
get(url: string): HttpPromise;
}
} /// <reference path='AngularModule.ts' />
/// <reference path='BookModel.ts' />
module Books {
export interface Scope {
books: Model.Book[];
}
export class Controller {
private httpService: any;
constructor($scope: Scope, $http: any) {
this.httpService = $http;
this.getAllBooks(function (data) {
$scope.books = data;
});
var controller = this;
}
getAllBooks(successCallback: Function): void {
this.httpService.get('/api/books').success(function (data, status) {
successCallback(data);
});
}
}
} /// <reference path='AngularModule.ts' /> /// <reference path='BookModel.ts' />
export interface Scope {
books: Model.Book[];
}
describe('myApp', function() {
var scope;
beforeEach(angular.mock.module('myApp'));
beforeEach(angular.mock.inject(function($rootScope) {
scope = $rootScope.$new();
});
it('...')
}); describe('Remote tests', function() {
var $httpBackend, $rootScope, myService;
beforeEach(inject(
function(_$httpBackend_, _$rootScope_, _myService_) {
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
myService = _myService_;
}));
it('should make a request to the backend', function() {
$httpBackend.expect('GET', '/v1/api/current_user')
.respond(200, {userId: 123});
myService.getCurrentUser();
$httpBackend.flush();
});
}); (function (module) {
var myController = function ($scope, $http) {
$http.get("/api/myData")
.then(function (result) {
$scope.data= result.data;
});
};
module.controller("MyController",
["$scope", "$http", myController]);
}(angular.module("myApp"))); describe("myApp", function () {
beforeEach(module('myApp'));
describe("MyController", function () {
var scope, httpBackend;
beforeEach(inject(function ($rootScope, $controller, $httpBackend, $http) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
httpBackend.when("GET", "/api/myData").respond([{}, {}, {}]);
$controller('MyController', {
$scope: scope,
$http: $http
});
}));
it("should have 3 row", function () {
httpBackend.flush();
expect(scope.data.length).toBe(3);
});
});
});