Calling rest API within AngularJS & restangular - angularjs

I have an API method that I need to call in my UI, and unsure the best way to do so. Can anyone point me in the right direction? Using restangular and angularJS. Should I create a service and then call the API Inside of it, and then reference that in my controller? Please let me know, thanks.

Maintain service layer, dao layer, view layer separately is the best practice.
You may look the Design Pattern.

The way I am doing is like this.
1 .First make an shared service first (shared.service.ts).
Create a method inside the service,there you call the rest api
getData():Observable<any>{
return this.http.get(localhost:8000/api/data);
}
or
getData(){
return this.http.get(localhost:8000/api/data);
}
Inject the service into the component where you want to use
constructor (private sharedService:SharedService){}
Subscribe to the method that is defined in the service to the function where you want to call the api
this.sharedService.getData().subscribe(response => {});

Related

Construct - make a function call to web page and give a callback function to be called later

Is it possible in Construct2/3 to make a function call to web page and give a callback function name to web page so its called later?
Use this syntax: c2_callFunction("get_score")

How to share api response throughout angular 2 application?

I am calling REST API through service and using that service to get response in root component. So now I have api response(JSON object) in my root component and I want share that object in child component. How can I use that object in child component without calling service again?
In short, I don't want to call sever many times for same data (call api only once) and use that response throughout angular 2 application.
Could you please suggest how we can achieve this?
You can do this by using get and set. Create a public variable with get and set.
Fisrt time when you get response simply set into that variable and after that call get method to retrieve same data.
public listData;
set listData(value){
this.listData = value;
}
get listData(){
return this.listData;
}

CakePHP how to log API requests globally?

I have an API based on CakePHP, controller with name AccessLogController is responsible for saving access log into the database.
Question is:
What is the best practice for global logging in CakePHP?
I thought that I will call AccessLogController method from inherited AppController in before filter callback like this:
public function beforeFilter() {
$accessLogCtrl = new AccessLogsController();
$accessLogCtrl->add($param1, $param2);
}
But i not sure that is it a good way how to do it..
Many Thanks for any advice..
That's not how you should use controllers, never ever, except maybe in your test suite!
That being said, using AppController::beforeFilter() to globally log controller action requests is generally fine, just make sure that you always invoke parent::beforeFilter() in case you are overriding the filter in an extending controller.
However, you should definitely refactor your logging functionality into either a utility class, a component, a model, or even directly into AppController, depending on how it's actually logging things, and if you need to use it in places other than in AppController.

How can I update the view after each API call

I am new to angular and I am building an app where I want to make multiple API calls and update the view as the data from them comes by. I do not want to wait for all the api calls to be completed to update my view and my api calls are not dependent on each other. Some of the API calls takes more than a minute to return the data.
I was thinking of using $q.all since I can start multiple asynchronous tasks, but I can't update the view after each one is completed. Could someone please point out how I can build this ?
Should I just use $scope.$apply in the success block of my $http call ?
My progress so far LINK (this was different issue I had, but the code is the same)
It's a bit hard to understand your model and what you're trying to achieve from you question, but you might want to use something like $broadcast() and $on().
So you'd broadcast an event when you're API has finished downloading:
$scope.$broadcast('API-download', data);
and then listen for it elsewhere and update your view
$scope.$on(
'API-download',
function(data){
processData( data );
}
)
That syntax might not be perfect, and as you have multiple API calls you'll need to broadcast different events like 'API-product-download' and 'API-catalogue-download'

CakePHP - centralizing controller logic

Using CakePHP, I am finding that I'm duplicating some code between controller actions. I have a dozen or so actions (belonging to various controllers) that all need to run the same query and set() the same 10 variables for the use in a particular layout. They also need to handle any errors in the same way and render an error page.
I know that components are intended to centralize logic used among controllers, but in my case, this logic needs access to the set() and render() methods of the controller. What is the suggested approach to this situation?
Thanks, Brian
Put the logic in your AppController class which your controller should extend from.
Check out the docs: http://book.cakephp.org/view/957/The-App-Controller
Ended up rolling my own sort of business logic layer on this one. Example below. Thoughts/comments welcome.
class MyController extends AppController {
public function my_action() {
// The BLL class is specific for this action and gets the entire
// controller so has access to the set() method as well as components.
$this->Bll = new MyActionLogic($this);
$this->Bll->do_whatever();
}
}

Resources