Call Controller method from view in EXTJS 4 MVC - model-view-controller

I am new to EXTJS 4 MVC architecture . I have one function in controller which i want to call from my view. How can i call that function?

You can send a message: YourApp.fireEvent('my_event', options) and listen it inside of your controller. But it's not right.
If you're using as you're talking mvc you should not write code in view. (Note: There may be a code, for example column renderer code, not business logic). View should be just a component group. To create component event listener use control method of Ext.app.Controller.

You can do it like so:
You can put your controller as a global variable in your app.js like so:
Ext.define('MySharedData', {
my_Controller:Object
});
In your controller:
MySharedData.my_Controller=this.getController('<ControllerfolderPath>.controllerName');
then call the function that you want, from your view:
MySharedData.my_Controller.my_Function();

Related

Laravel include view

I want to include a view like so: #include(user.myview), but within this view I need UserController logic. So I thought about calling a route: #include( route('user.route') ) which calls a Controllerfunction and returns the view but that isn't working. Any Ideas how to deal with this problem?
You need to create view composer and use it to get the data.
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
Simply add a link in you view and include it in your desired location.
Link will have a route.
On clicking the link, controller method can be called. e.g. show_link.blade.php
In your show_link.blade.php view:
<a href= {{route('route-name')}} > Click here</a>.
This route will call a method via .
Route::get('/call/method', 'controller#your_method_name')->name('route-name');
In controller, method your_method_name that will look like this:
public function your_method_name()
{
return "show what you want to";
}

Open component view from code behind

I am using mvc 6 and I would like to "open" component view by using some button onClick.
I can display it by using
<div id="DialogDiv">
#Component.Invoke("MyComponent");
</div>
but I would like to do it so that some button onclick method would call that invoke method and give that div as a reference. Is that possible?
I assume that you want to render component by using ajax request. Since MVC6-beta7 you can do this by utilising View Component Action Result:
See:
https://github.com/aspnet/Mvc/issues/2338
https://github.com/aspnet/Mvc/releases/tag/6.0.0-beta7
The idea for your scenario will be to use JavaScript and call action in controller which will return ViewComponentResult and then render result of that action into your page. You can do this by using jQuery, so it will look like:
$.get("/YourController/YourAction",function(data){
$("#DialogDiv").html(data);
});

Call another.js view from alloy.js titanium

have 1st view = index.js
2nd view = another.js
global file alloy.js
I have called the global class (alloy.js) method from index.js and now i want to move to another.js view but after httpRequest method finishes (which is declared and implemented in alloy.js).
---alloy.js---
Alloy.Global.httpMethod=function(){
//xml response is achieved, now from here i want to navigate to another.js view
}
---index.js---
Alloy.Global.httpMethod();
---another.js---
//some UI objects.
The first thing you want to do is create a controller, your another.js must be a Controller, it can be created by doing Right click on app and creating a new controller from there.
Once your controller is created, you can easily do what Phil said in the comment. i.e
Alloy.createController('another').getView().open();
This has to be placed in your onload function of HttpClient.
Hope it Helps.

How to get access to Current view model methods from Shell.js

I have a button in shell.html, on click of that button I need to post the current view model.
I can determine the current view model's Module Id, but how can I get a reference to it so that I can invoke its methods from Shell.js
For eg, if my current view model has a method called "SubmitApplication"
I would like to call or trigger this method from Shell.js on click of the button in Shell.html
Please help.
Thanks
Consider using Durandal's event system for cross module communication.
http://durandaljs.com/documentation/Leveraging-Publish-Subscribe.html
By default app has event capabilities included, which would allow you to do something along the following line:
shell.js
submitOnClick: function(){
app.trigger('application:submit', payload);
}
viewmodel
app.on('application:submit').then(function(payload){
//do something with payload
});

Custom MVC Framework: How do I propogate data to a view?

I am creating a custom MVC framework.
I verrrrry loosely modeled it after the codeIgniter framework, but it's ground-up custom for the most part.
I'm at the point where I have URL's routing to the appropriate controller actions, but I'm stuck at the point where I generate a view that can utilize data generated by the controller.
I have views defined (static HTML with inline php ready to populate dynamic data), and I have the destructor of my base controller require()'ing the view in order to populate the browser with the view... here's the code:
public function __destruct()
{
if ($this->bSuppressView === false)
{
require(APP_PATH.'views/layout/header.php');
require(APP_PATH.'views/'.$this->sController.'/view.'.$this->sController.'.'.$this->sAction.'.php');
require(APP_PATH.'views/layout/footer.php');
}
}
Basically, when the controller is done executing, the teardown process of the base controller will then include the global header view, the controller's action's view, and then the global footer view, which should populate the webpage with everything for the URL that was requested...
HOWEVER, I cannot access any globally defined variables from the embedded php in the view code. In my bootstrap class, I define a bunch of local variables such as my config variable, etc., but the view seems to consider those variables undefined. Additionally, i'm unsure how to allow the view to access data that the controller may have generated. Where do I "stick" it to make it available to the view?
Let me know if this isn't clear, and i'll update.
Thanks!
UPDATE: I've discovered that while doing it this way, the "environment" of the views is within the controller object, which, as far as I can tell is a great thing! I don't have to propogate anything anywhere but in the controller, and I can use "$this->" in the views to get access to anything public or private from within the controller class!!!
That leaves the question: is this "normally" how it's done in MVC? What's the BEST way to propogate a view? I think this will suit my purposes, and I will post back if I discover a limitation to just treating the embedded view php as "within the scope of the calling controller"...
The way this is generally done, is that the view is actually an object. You pass that object you're variables, and that view object takes the template you gave it, includes it so that it's in the current scope, and grab the output into a variable using output buffering.
To give you a basic idea:
// controller object
$this->set('key','val');
$this->render('mytemplate');
// controller base class
$view = new View;
$view->setData($this->getData());
// view class
class View {
....
function render() {
ob_start();
include $this->resolveTemplate();
$out = ob_get_contents();
ob_end_clean();
return $out;
}

Resources