@Injectable() export class BookService { url = "api/books"; constructor(private http:Http) { } getBooksWithObservable(): Observable<Book[]> { return this.http.get(this.url) .map(this.extractData) .catch(this.handleErrorObservable); } addBookWithObservable(book:Book): Observable<Book> { let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); return this.http.post(this.url, book, options) .map(this.extractData) .catch(this.handleErrorObservable); } private extractData(res: Response) { let body = res.json(); return body || {}; } private handleErrorObservable (error: Response | any) { console.error(error.message || error); return Observable.throw(error.message || error); } }Then, in the controller, I declared a variable called kanbanData (JS Object) and use that as data source.For displaying items this works fine, but I need to capture the "add item" to call the service.Should I create a customAdaptor and then call the service form there? Do you guys have an example?Thanks!
|
routeTemplate: "api/{controller}/{id}" |
|
Kanban.component.html
<ej-kanban [dataSource]="kanbanData" …> // Render Kanban control
</ej-kanban> |
|
Kanban.component.ts
constructor() {
let customAdaptor = new ej.WebApiAdaptor().extend({ // Extend web API adaptor
batchRequest: function (dm, changes, e, query) { // Automatically called when update/add/delete operation performed in Kanban control
var response = { // Specified response variable
changed: changes.changed,
added: changes.added,
deleted: changes.deleted,
action: "batch",
table: e.url,
key: e.key
};
if (query)
this.addParams({ dm: dm, query: query, params: query._params });
return {
type: "POST", // Send Post request to the controller
url: dm.dataSource.url, // send url (api/Employee) into post request
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(response) // Response variable was passed to controller
};
}
});
let datamanager = new ej.DataManager({
url:"http://localhost:63187/api/Employee", // Here, Kanban is a controller name and don’t specify crudURL
crossDomain: true,
adaptor: new customAdaptor() // Specified custom adaptor
});
this.kanbanData = datamanager;
} |
|
EmployeeController.cs
public class EmployeeController : ApiController
{
static readonly IEmployeeRepository repository = new EmployeeRepository();
// GET api/<controller>
[Route("get")]
// GET api/values
public async Task<object> Get() // Call when load Kanban
{
await Task.Delay(1000);
var queryString = HttpContext.Current.Request.QueryString;
var data = repository.GetAll().ToList();
return new { Items = data, Count = data.Count() };
}
public Employee GetEmployee(int id)
{
Employee emp = repository.Get(id);
if (emp == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return emp;
}
[Route("post")]
public async Task<IHttpActionResult> Post([FromBody] DetailData emp) // Call when update, add and delete operation performed
(Here, passed data variables(emp) and class type(DetailData) specified in same parameters. Otherwise this post was not called)
{
…………… // You can perform database operation
return Content(HttpStatusCode.OK, emp);
}
public class DetailData // Here specified vaiables on batchRequest post data variables
{
public List<Employee> Added { get; set; }
public List<Employee> Changed { get; set; }
public List<Employee> Deleted { get; set; }
public string action { get; set; }
public string table { get; set; }
public string key { get; set; }
} |