Using a command object chain command with a view - grails

I am using grails v2.3.3 and I am trying to work with a set of instances of a command object across two actions.
In the first action I create the set of command object instances which I then display in a view to be edited.
I then use a link in the view to submit this data to another action in the same controller that needs to access the updated set of command object instances.
I have looked at the 'chain' command which enables command objects to be accessible across different actions but it seems to offer a direct link from one action to another without the option to display a view and enable some user interface.
I cannot see how to implement this with a view in order to update the contents of these command object instances which then get sent to the 2nd action to be processed.
I have the command object set to a 'session' scope and am surprised that by default it is not accessible across all actions of the controller anyway - what does the scope mean?
-mike

The normal way of doing this is to recreate the command objects from posted form data in the second action. This means that the view's form must include all of the fields of the command object either as visible (editable) or hidden (pass through) input fields. Grails command object binding support will automatically populate new command objects if you place them as parameters to the action. Also, you can always manually construct new command object instances using the "params" map values which contains all of the posted form fields.
See the sections on Command Objects and Data Binding in the Grails manual for details and examples.

Related

SAPUI5 when to bind an initial filter to a control

I wish to bind a filter dynamically in the controller of a xml view to a control in the view. I know how to do this but I don't find a proper way to do this initially.
E.g. I defined a ComboBox on the view and gave a binding path to the oData model (in the view). Then in the controller I wish to set a fiter on the items of the ComboBox. I tried in onInit of the view but there the binding is not yet set in the ComboBox object.
I solved it by doing a lazy loading and set the filter in the "loadItems" event. Is there any way to do this by not using lazy loading? Did not find a proper event or standard hook.
Thanks.
So the binding is not available in oninit()... Where exactly do you create the binding then the very first time??? Where ever you create the binding the first time you can also apply the initial filters.
For example, in the controller inside onInit you call this.byId("myComboBox") to get the control. Then you call oComboBox.bindItems({...}) and also pass the initial filters. So bindItems(...) also accepts filters. If the filters change you can simply call oComboBox.getBinding("items").filter(...) to update the filters. Check the worklist app tutorial for some details...
By the way, you could also directly specify the binding including filters in the XMLView directly. See my answer here for details.

Pro/Con of reusing single CommandObject instance between multiple forms/actions?

Are there any disadvantages to using a single instance of a grails command object across multiple actions?
I.E. you have a several pages with forms in a process and thus create a single grails command object class and only use a single instance of the class throughout the process.
Presume the instance is stored in the flow of a webflow, thus accessible throughout the process, and when validate is called it's provided with a list of properties to validate instead of validating all the properties. The command object properties are populated like obj.property1 = params.property1 not with bindData() or as a parameter.
Is this a good fit for a grails command object or should a command object for each form be created?
Edit:
I guess what I'm looking for is a Pro/Con list of using one command object for one entire process with multiple forms vs. multiple command objects.
I would prefer to use single CommandObject for each form and only reuse the CommandObject in multiple forms if the forms are for same purpose but in different pages.

WebMethods binding table data

I have a simple WebMethods user task which has some simple string data and also a document list in the input document. This document list has 4 fields.
The task view has fields for the strings and an aysnc table for the document list to which I have added a 'Add Row' table button.
The problem is the data. Any existing rows that are edited are reflected in the pipeline after the task has been completed but if I add a new row along with values, then the new row is ignored. I have bound the data directly to the table and also tried a content provider to handle the data but neither method passes the updated table data to the output.
Thanks
Rich
Richard,
Try with below options.
As the table is getting edited on UI - remove the sourceArray/sourceList binding of the content provider/ table provider. (To Populate this provider with list of values,
create a new action
Using data flow implementation assign the provider sourceArray/sourceList to the actual source data list/Array.
Invoke this service before rendering the page (ie., in Initialize method if at the loading of page).
2.Change the provider class to "com.webmethods.caf.faces.data.object.ListTableContentProvider" manually inside the source code and all respective places. (To verify open bindings view and expand the provider. It should not show the "Refresh" action.)
3.On submit form button invoke a action with assigns the provider sourceArray/sourceList to the Target data array/list.

MVC4 - getting list of fields in View

Is it possible to safely programmatically get a list of fields that are in the View that has just posted back to a Controller?
I noticed a problem with the default implementation of the scaffolding, in
DB.Entry(model).State = EntityState.Modified
DB.SaveChanges()
The problem is that if I haven't included a field to be edited in the view, it is being overwritten by the default value of the field that .NET assigns when creating the object. eg. If I have a User class with ID, Email and PasswordHash and I want to allow the user to update their Email address only, if I don't include anything for the PasswordHash field, it is reset to NULL as it is passed into the controller as NULL. At the moment, I am working around it by retrieving the current object from the database and updating only the fields which I know are in the View from the model passed in. That isn't such a problem for a small table, but I would like to have a general solution that I can apply across the board, especially for large tables which may during development and I don't want to have to update the code every time.
I know that I could loop through the POST variables and examine them to see what has been posted, but that creates a security issue as the user could inject additional fields that I don't want them to edit. I suppose I could explicitly exclude ones that I don't want them to edit, but then again, I would rather not have to list those if I can avoid it as it is an extra thing to maintain.
I think that there are 2 problems here and I'm not sure either are solvable...
Getting the View that posted back
Establishing which fields are included in that View (I might need to construct it again temporarily to do that?)
I suppose that I can probably get away with ignoring the first one as I could just only ever use that method on the Controller for a single View. That is still a little less neat than I'd like, but it does reduce the issue to just establishing which fields are in the View.
If a view needs only certain properties, create an interface with only those properties. Use this interface in the HttpGet and HttpPost methods.
And then you can use something like AutoMapper to map the viewmodel to your entity.

How should I build a list of items that link to different controller actions?

I have an ASP.NET MVC web app and on one page I wish to show a table of items which will include a link to the detail for that item. Pretty basic. However, the items I wish to display in the list are all subclasses of a common base type and so the links need to reference different controller actions.
So, my question is: where/how should I construct those links (see example below)?
I can achieve my goal already in a number of ways (e.g. construct route while mapping business object to view model, use flags or magic strings in the view model, have a single 'base' controller action that selects the appropriate view, etc.) but none seem particularly 'clean' so this is more a question about best practice than mechanics.
By way of example, suppose my 'Assignments' table looks like:
Id <OtherCommonFields> Type Name Link
1 <some data> Event Some event /event/1
4 <some data> Task Some task /task/4
3 <some data> Event Another event /event/3
2 <some data> Task Another task /task/2
5 <some data> Event One more event /event/5
Where 'Tasks' and 'Events' are both subclasses of 'Assignment'.
the short version i would say is to create a new actionlink extension method called AssignmentLink, where you pass in the id and the type.
inside the assignmentlink method, do a switch on the type and return the correct url in the form of an actionlink.
that way if you add different kinds of assignments or whatever, you control everything in one central helper.
in a previous mvc app, hyperlinks caused us a lot of headaches. when we renamed a controller, or moved a controller to a new area etc, updating all the references to that controller took forever. so in our most recent project we wrapped up all hyperlink logic into an object called "linkdata"
we then created extensions for our linkdata object, with any logic in the helper or extensions.
http://codingsmith.co.za/extending-the-actionlink-helper-in-mvc/ if you're interested, but you can get away with far less in your example, just a simple extension will do it.

Resources