I have extended class A in class B and i want to use class A methods in class b.
To share a method between two controllers, place the function in AppController.
To share a group of method related to a single feature, create a new component and use it in both controllers. It's the proper OOP way to do it.
Related
Here is my question: What is the difference between these two declarations:
Whats the matter if I declare it on top of my component and what if within it?
Thank you in advance.
Functions that are created inside class components are usually referred to as methods.
If your function doesn't depend on class instance properties state or props and is independent of the component you can define that function outside the class.
But if your function need access to the class state, prop or any other class methods, you'll need to define it within the class component so you can use this.state or this.props.
So, thumb rule would be that if you need any access of the class component instance i.e. this within your function, you will need to define the function inside the component.
You are probably wondering what I mean by brothers components, well it's simply 2 different components that have the same parent component.
I am using angular.js 5.11
Let's say I have a parent component with a child1 component and a child2 component. I have a variable vm.active in my child1 component and I wish to use it (in an ng-if if you're wondering) in my child2 view.
Any ideas ? Was thinking of doing two way binding in both 3 components ? What do you all think ? Or maybe considering they are from the same state, probably as a stateParams ? Please let me know if you have any questions
This is the perfect scenario to create a service. Remember controllers have unique instances, but a service passes a common instance around.
https://www.w3schools.com/angular/angular_services.asp
Do not use two-way data binding in this case because you can accomplish the same in a more efficient way. Two way binding setup requires framework overhead.
One approach:
Setup one way binding of the variable (say x) in the Parent component to both of the children.
When child1 makes an update to the variable, pass in an observable reference that the parent can receive. The parent can set the value of x accordingly. Now both the children can see the update.
Here's another:
Use a singleton service. Save the variable in a related service. Use the getters and setter methods to retrieve and update the values.
Well ended up using $scope.$parent.$broadcast in first component and $scope.$on on the second component if anyone is wondering :)
I have callback functions in AppModel.php, and also for some models, is there is way to automatically call the app model callback with (preferably before) the current model's callback is being called.
e.g. lets say I have beforeSave in AppModel, for each beforeSave function in my models I have to put
parent::beforeSave($options) in it. Now, can I make it at once for all models, so I will not have to put in each callback in each model.
Thanks
Nope.
Because your Model extends AppModel, any of these callback functions will override the parent function. You will always have to manually call the parent function.
I have a set of functions shared between all my controllers and I'm doubting whether I should place them in a component, loaded from every controller (or from AppController), or add them in the AppController class (with visibility set to protected), so all the controllers inherit them.
Which is the better?
Creating a component is recommended, IMO. Components are lazy loaded and also help keep your code look clean. Also in use cases where you need access to some sort of model data, you DO NOT want to load models and call them from AppController!
Can a cakephp component use a view? if yes then please explain how?
Components in CakePHP do not use views.
You can use component methods in controller actions, and those actions can have views, though.
M - models, behaviors
V - views, elements, helpers
C - controllers, components
things from one group should not be used in the other.