I have class AbstractRepository and class UserRepository extends AbstractRepository. I am declaring super() in my constructor but the error is _http is not defined. How can i insert $http in class UserRepository.
AbstractRepository:
import _ from 'lodash';
import 'rxjs/add/operator/map';
class AbstractRepository {
_http;
_config;
_Model;
constructor($http, config, Model) {
'NgInject'
this._http = $http;
this._config = config;
this._Model = Model;
}
/**
* Gets all instances of repository entities
*
* @returns {Observable<Model[]>} - List of observables of all entities
*/
getAll() {
const url = this._config.url;
return this._http.get(url)
.map(dataList => dataList.json())
.map(dataList => _.map(dataList, item => new this._Model(item)));
}
/**
* Gets a specific item of repository entity
*
* @param {number} itemId - ID of an item to find
* @returns {Observable<Model>} - Observable of repo entity item
*/
getItem(itemId) {
const url = this._config.url + itemId;
return this._http.get(url)
.map(item => item.json())
.map(item => new this._Model(item));
}
/**
* Creates a specific item of repository entity
*
* @param {Model} item - Item to create
* @returns {Observable<Model>} - Observable of recently created item
*/
createItem(item) {
const url = this._config.url;
return this._http.post(url, item)
.map(item => item.json())
.map(item => new this._Model(item));
}
/**
* Updates a specific item of repository entity
*
* @param {number} itemId - ID of an item to update
* @param {Model} item - Wanted updated item
* @returns {Observable<Model>} - Observable of repo entity item
*/
updateItem(itemId, item) {
const url = this._config.url + itemId;
return this._http.put(url, item)
.map(item => item.json())
.map(item => new this._Model(item));
}
}
export {AbstractRepository}
UserRepository:
import { USER_REPO_CONFIG } from '../repository.config';
import { AbstractRepository } from '../abstract.repository';
import { UserModel } from './user.model';
import 'rxjs/add/operator/map';
class UserRepository extends AbstractRepository {
_http = $http;
constructor() {
'NgInject'
super(_http, USER_REPO_CONFIG, UserModel);
}
/**
* Gets the user by ID
*
* @param {number} userId - ID of a user to find
* @returns {Observable<UserModel>} - User model instance
*/
getUser(userId) {
return this.getItem(userId);
}
/**
* Creates the user
*
* @param {UserModel} user - User model instance
* @returns {Observable<UserModel>} - User model instance observable
*/
createUser(user) {
return this.createItem(user);
}
/**
* Updates the user
*
* @param {number} userId - ID of a user to update
* @param {UserModel} user - User model instance
* @returns {Observable<UserModel>} - User model instance observable
*/
updateUser(userId, user) {
return this.updateItem(userId, user);
}
}
export {UserRepository};