Block in sidebar - where to put the logic? - forms

I would like to create a jumpbox block with some form and put it within the layout sidebar. The form will be have an entity select and exactly the Go button.
Base on the documentation I need to render the form template by using {{ render(controller(...)) }}, but I really don't where tu put the form logic.
It is good to create a method in the controller that use entities from the select? but it looks I need to create two methods, the first one for form rendering (without any route) and the second one for the form submit request (with route for "POST" method)?
Can somebody provide me some tips how to do it right way?

First you will need a controller as you sad, this will render this part of the sidebar, and the action should be the same controller, so just simply create a router for it.
And you were also sad right, create a hidden field and set there the current route. But this is the tricky part, cause when you call this "sub" render, inside the controller the route will be always, what is the route for the controller, so what you need to do, when you render the controller you need to pass in a variable what is the current route, what you can do easily by passing the {{ app.request.attributes.get('_route') }} variable value, what is the NAME of the route, and then in your controller, at the end you return a new RedirectResponse($this->generateUrl($url)).
And both, the render of the form and the "process" can be in the same controller, or if you prefer it, you can take it apart, but I would use only one, and you can test from the request, what is the current method, if POST then you will search for the variable and set the session/cookie/ what do you have.
EDIT:
Even though you didn't like, you have to say it's a good answer, but here is an other one.
The action should be always the current route, and basically you need to set up a request event listener. Check there if it's a post method and if yes, then look for your specific key, and there you go. Both is equally good and I used both of them.

Related

Create / Edit / View the same form with Angular

I am creating a slightly elaborated form with Angular. This form can be submitted, then modified or simply displayed (with everything in read-only for example).
For now I have 3 templates with 3 controllers for each action (submit / edit / view) and the form is added as a partial (ng-include). The form has also its own controller. Is it the right way?
Also, should I make the form's controller the children of the templates' controllers or the opposite? I am using the same model for each action behind the form and I guess it should be injected through the template's controller.
It's my very first attempt to do this and I would like to have a few advice's and hints since I am afraid of going the wrong way. Thank you!
yo can use different template for each of them with single controller, different controller for each of them is not a good idea because there may be some common function in them then you need to write that function in each of them.
you can use common model for them,i am doing the same.
I see this is promising solutions to what you are looking for
can you try this http://vitalets.github.io/angular-xeditable/#editable-form

AngularJS: When user action impacts both the model and the DOM

This is for an AngularJS app. I have a custom directive that depends on a service.
What I'm really curious about is the "angular way" to deal with a user action that impacts both model and DOM. Some example code:
HTML:
<form foo-places>
<!--other stuff -->
<span ng-repeat="place in places">
<button ng-click="removePlace(place)">remove {{place}}</button>
</span>
</form>
JS:
angular.module('foo.directives', []).directive('fooPlaces',
function(placesService) {
return {
controller : function($scope) {
$scope.places = placesService.places;
$scope.removePlace = function(name) {
placesService.removePlace(name);
};
$scope.$on('placesChanged', function() {
$scope.places = placesService.places;
});
},
link : function($scope, element, attrs) {
//code to do stuff when user removes a place
}
}
})
When a user removes a place (by clicking a button), I also need to do stuff to mess with the DOM, for example, scroll the window to the top, etc. It feels weird to have a function in the controller that deals with the model and then another function in the directive that does the DOM stuff...but both based on the same user action.
Am I over-thinking this or really missing something? How should I handle a single user action that deals with both model and DOM?
When you are dealing with AngularJS you might have heard the phrase "The model is the single source of truth". If you understand this part, then the rest of the things fall easily into place. This is the "Angular way".
When the user interacts - he is not interacting with the DOM or the view. He is interacting with the model. The view itself is just a "view" of the model. There could be other views of the same model - which is why the model is the single source of truth. Now, what angular allows you to do is make changes to the model when the user interacts. You make these changes and because the model has changed, the view's start reflecting the changed state of the model.
Also, just to emphasize the separation of concerns - a directive should rarely, deal with a service directly. A directive is a piece of the DOM, which means it is a piece of the view. A service generally has something to do with business logic or represents a model. In MVC or MVVM you dont directly make the View interact with the Model. You always use the ViewModel or Controller in between. This keeps the dependencies to a minimum.
Your ScrollToTop could be a service that you call from your controller (look at $anchorScroll which is a service in Angular ). It doesnt do what you want, but its a scrolling service, which is how you need to implement yours too.
EDIT :
To clarify, you dont generally do DOM manipulately stuff in services. The scenario where you could consider DOM manipulatey stuff in the service, is when, what you are trying to do does not belong to any particular html element, but something that needs to happen on your app level.
Let me explain that. For example, if you are attempting to do something like a dialog / modal window - In angularJS, you would think, the ideal place for something like this is a directive since it is a generic UI component. But if you think about it, a directive in AngularJS is something associated with an element. You always associate a directive with a html element. But as we have seen, a dialog isnt something that you attach to an element, but rather something that is global in nature. This is probably an exception.
The same also holds true to some $window and $document related stuff ( scrolling for example ). These dont belong to any particular element (if you want to scroll inside a div, it should be a directive ), hence they need to be a service. Also, this is a service you could probably inject into a directive. Say each time your directive is triggered you want to scrollToTop or open a dialog. You could inject these kind of services into your directives. The kind of services that you probably should not inject into a directive are services that are associated with business logic. Treat a directive as a re-usable UI component.
Ofcourse, you could create a higher level component ( the stuff you are trying ) which creates a DSL, but then you need to know exactly what you are doing. Until then, I suggest you stick with the plain old controller, directive and services and each managing their own concerns.

Zend framework --- Action code before rendering view

When I use zend framework, how to run action code in controller before rendering view?
Now I use 2 action functions,
The first one have no view. The second one have the expected view.
When the first function is called, at the end of the function it will be redirected to call the second function.
Although I find this method works, the user cannot go back to last page by browser back button.
i don't understand your question so much but ...
Controller is always run before render action. Its a logic which running before render is called. I dont know from your question what you really need =( try be more objective pls. If you need run any code before action in controller on all actions in controller, you use init or preDispatch functions.
http://zfreak.wordpress.com/2011/04/14/usage-of-init-vs-predispatch-methods-in-zend-front-controller-zend-framework/
If you need 2 separated logics, you can make own function or class in your project and call it or create instance anywhere you need. If your problem is with any render as ACL restrictions... i use own function in controller which retrieve error_access_page and after call $this->_helper->viewRenderer->setNoRender(). If my users not have access, this show error_page and no render action's phtml file.
hope help =]

ASP.Net MVC 3 - Single controller, single action and multiple views

I would like to know what is the best method to have an action return different views. Let's say you have a form for submitting data, but you want to choose the view depending on what data is submitted. I would prefer not using a redirection, since there is stuff I want to be displayed in the data that is posted.
An example of this would be to have an Edit form that displays a Details view when clicking on Save, but without using a redirection.
I know this could be done with a single view containing a conditional if statement to display this or that, but there are cases where I would prefer my views to stay simple without too much code in them. If the controller could just choose the view to display once the data is posted, this would be great.
There is an overload to the View() method that allows you to specify the name of the View you want to return.
return View("DetailsView", model);
You should be using the Post/Redirect/Get pattern. You can still "display stuff." You can pass an ID on the URI and look them up in the new GET or use TempData.
Attempting to circumvent Post/Redirect/Get is not a good solution. Among other things, it breaks the back button.

Returning object model to a view, that is handled by different controller

how can I pass an object model to a view, that is partial view on a master page?
regards
You might consider creating an another object that more closely represents the view you are trying to render.
Let's say i have an MyDomain.Order object, so I make a view page that looks something like ViewPage<MyDomain.Order>. Now, let's say that I have a menu that is driven off of a logged in user, as example. It wouldn't make sense to have menu as a property of MyDomain.Order. I would create another object, specifically for the view, call it something like OrderPageModel and have MyDomain.Order and List<MenuItem> as properties of this new object, my view being set up as ViewPage<OrderPageModel>.
The other thing to consider might be something like Html.RenderAction(). Same scenario, I have a view, and as you mention in your question, it has a master page, and as in my example, lets say it hosts a menu common to your site. You could create a partial view (UserMenu.ascx) and a controller (SiteController.cs) with an action (UserMenu) that calculates the items for the menu. Inside your master page, you can then call <% Html.RenderAction("UserMenu","SiteController") %>.
I would use the first example if it could be something made for a particular view: just make it a part of the model. I would use the second example if it was something more generic to the site, like a menu.
You could specify the location of the view:
return PartialView("~/Views/SomeOtherController/SomePartial.ascx", someModel);
Best bet here is RenderAction over RenderPartial. Your child controller can easily figure out if the user is logged in and render the right partial rather than making your master page worry about these details.