I have a function that returns a string in Controller. So how can i invoke that function when a button is clicked in view?
Perhaps this function would be better suited in your model, especially if it perhaps refers to model properties.
Therefore instead of passing a string value to it, this can be accessed within the scope of the function:
#(model.Function())
If you function is generic, then instead of storing this in the controller, or model, I would recommend storing it in a different class altogether in a seperate namespace:
#(Logic.StringFunctions.Function(model.myString))
As others have pointed out, there may be better options, depending on what it is exactly that you're trying to do. But if your desire is simply to call a method with the click of a button, the simplest way I know of to do that is:
<button type="button" onclick="window.location.href='<%=Url.Action("MethodName", "ControllerName") %>'">
Hope that helps!
Related
im newbie here, I have a simple question that keeps me up all night,so is it posible to double render in livewire like a normal controller?
like this
//Home Controller
public function index(){
return view(home.index);
}
public function dragonCat(){
return view(blah.index);
}
Not in the way that you're showing (using separate methods), no. And that's also not how Livewire is meant to work. However, if there's a reason why you would want that, perhaps I can give you a suggestion on a better approach.
Just for info, a controller is a bridge between your HTTP routes and your views. While you technically don't need a controller (as the routes you define just need a callback), you use a controller at first to keep things organized (e.g. all Product model related routes) as well as to define more complex logic that would otherwise clutter your web.php.
A Livewire component is a "live" piece of frontend, a reactive component, basically a very complex and high quality AJAX wrapper to communicate between the client and the server. It uses DOM diffing to ensure minimal payload needed. If you were to change the view in the render method, maybe that would work, but it would most likely be very slow as it would mean replacing the whole view.
So where a controller "technically" has (or can have) more than one return view(), you would always only ever call one at the same time, since it would be related to the current route you're on. A Livewire component can also only have one return view(), however there is nothing bad about having more than one Livewire component on a single page.
For example I have an "action-one" in "controller-one", if it called from views of that controller, return is different when it called from views of another controllers; So I should know which view from which controller calling an action.
It is easier to just have a flag, as an optional method parameter that you would use to specify and check in your controller.
But it is better to keep shared code in a different method if possible and have different actions for different purposes.
If what you needed was something that would not necessarily need a different action, then just pass the action name or any other identifier you wish to use and then check and do the different stuff you need to do based on their identifier.
I've been looking around for best practices on where to put some helper methods that do all this calculation / sorting for me but I haven't found a definitive answer yet and was wondering if someone had some good insight for me.
Basically I have an action method that takes a string user input, finds similar words to that string, does a bunch of string manipulation, and then ordering to return an array.
I don't know whether I should have module in /lib, make a controller helper module, ... Where I'm looking for some feedback!
But essentially I just want to:
POST a word to a controller action method
Call a helper method or execute some logic on the word outside of the controller
Have that helper method or wherever that logic will be, return to me the result
Just put that method in your application helper, wherever it is called from it will return the resulting values after processing your logic, you should not over complicate things so the good old application helper is a good place to put the common method used in views and in your controllers
I wouldn't call a helper method from your controller. Personally I would use either a module/plain old ruby object or a concern to handle calculations. The controller stores the object in an instance variable so it can be used througout your views.
Also worth noting are decorators, you should take a look at draper: https://github.com/drapergem/draper
Following the tutorial how to create a joomla 2.5 component I'm stucked to pass arguments from view.html.php to my model.
$items = $this->get('TableData');
and my TableData model would expect to get the following arguments
public function getTableData($table, $index_column, $columns) {}
You can not do this using the view's get method. Instead you would have to grab the model into the view and call the function directly in the view:
$model = $this->getModel();
$items = $model->getTableData($table, $index_column, $columns);
Alternately, you could create different entry points in the model that would be able to figure these input options either from state information or preset. Many would argue that this would lead to a better application design, since using my code above is putting what should be model logic in the view.
This is just an addon to David's answer.
Because most of the data usually comes from POST / GET methods, depending on your application, you may want to look at how loadFormData() from loadFormData JModelForm or populateState gets overridden in specific Joomla components.
Basically the state of model is set directly from POST data, using JInput.
Also, although it's not a rule or something, 3 parameters is a maximum I would pass to a method. For flexibility I would rather pass an array with can be later extended without changing the method signature.
Given that there is file selection widget on the view and controller need to handle event of selecting file, should I rather write controller method:
public void fileSelected(String filePath){
//process filePath
}
or
public void fileSelected(){
String filePath = view.getSelectedFilePath();
//process filePath
}
The first approach seems to introduce less coupling between C and V: C don't know what exactly data does C need while handling given event.
But it requires creating a lot of verbose methods similar to getSelectedFile on V side.
On the other hand, second approach may lead to cluttered controller methods in more complex cases than in example (much more data to pass than just filePath).
From your own experience, which approach do you prefer?
The first approach is my favourite. The only difference is I would rather use an object (like Mario suggested) to pass arguments to the method. This way method's signature will not change when you add or remove some of the arguments. Less coupling is always good :)
One more thing:
If You want to try the second solution I recommend using a ViewFactory to remove view logic from the controller.
The first approach is the way to go;
public void fileSelected(String filePath){
//process filePath
}
The Controller should not care about how the View looks like or how it's implemented. It gets much clearer for the developer as well, when creating/updating the view, to know what an action in the controller wants. Also it makes it easier for method overloading.
Though, I don't know really how String filePath = view.getSelectedFilePath(); would work. Are we talking about parsing the View code/markup?
On the other hand, second approach may lead to cluttered controller methods in more complex cases than in example (much more data to pass than just filePath).
That's when you would create a View Model class (let's say we name it MyViewModel) to store all the properties that you need to send (may it be 10 properties) and then pass that in the action: fileSelected(MyViewModel model). That's how it's intended to be used and what the *ModelBinder's in asp.net mvc are there to help you with.
I think you need to look at this from a step back.
Worry less about how it gets in, and be more concerned with validation and error raising.
Tomorrow, your requirements could change and demand that you source the information via a different architectural approach. You could refactor the setup of [inputs / an input object] into a base controller class - or one of several classes for different controller domains.
If you focus on proper validation, whether within the controller (scrubbing) or outside of it (unit tests), then you perform more thorough decoupling though duck typing.
I would go with the first approach. It's reusable and separates concerns. Even if the method of getting the filePath in the future were to change, it won't affect your method's functionality.