How to pass a map from gsp to controller as part of params in Grails remoteFunction - grails

I am trying to send map from gsp to controller but the map is considered as a string in the controller
<g:remoteFunction
action="updateCart"
params="{startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:'${shoppingCart}'}"
update="resourcesSelectedId"/>
Here the shoppingCart is a grails map variable I am trying to send
Edit:
there was a typo in the code I posted above. Missed starting "{" in the params
params="{startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:'${shoppingCart}'}"
Updated my question as per my comments below
In my case shoppingCart is an object and it has, lets say for example, items and quantity of each item. I have some rules to be applied based on the items selected and quantity and determine the price for each item and show it back to the user. I want to do this processing the controller. Whenever user updates the cart I need to re-calculate and show it back to user. Is there any other better approach you would suggest to do this instead of passing the objects back and forth

Anytime you're using the params attribute, it has to be in the format of a Map anyway, which means including the [ ]. This also means you can exclude the ${ } from any values because grails will parse all these as potential variables.
<g:remoteFunction
action="updateCart"
params="[startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:shoppingCart]"
update="resourcesSelectedId"/>
However, keep in mind that you can't send objects. I'm not sure what shoppingCart is in your example, but it would only be able to be a simple value that can be represented as a String. Possibly you would want shoppingCart.id? Otherwise, that should get you going in the right direction.

I could able to overcome this issue by following below steps
converted the shoppingcart object to json string
Pass the json as part of the params
parse the json string server-side & process
pass the updated object back to gsp

Related

How to access object field in qaf step from stored variable

In my previous question I was looking for a way to access and store return value of the function in qaf step. I was provided with the following:
When create new user using "{'name':'user1','password':'user123'}"
And store into 'newUser'
Then system should have user '${newUser}'
Now, I'd like to know how to get value from object/collection stored.
If it is a simple object named newUser which has field Id. How would I pass Id on next step?
And, if return is List, how to get by index from stored list?
Resolved issue on my own. If anyone faces same unknowns, here is how I solved it.
For requirements to work around response data, parsing same stored objects in properties by specific fields or collecting data from other structures such as Maps or Lists, create common functions with #QAFTestStep annotation to get data for class member name, map by key or list by index and so on... Add those in common steps and then write stepname text in gherkin format with parameters specified. Let me know if someone needs help, always ready to help out...

Save multiple selected dropdown values into a single column in Grails

How to save multiple selected dropdown values into a single column in Grails?
Input will look like
<g:select name="item1Price" from="${1..10}"/>
<g:select name="item2Price" from="${1..10}"/>
<g:select name="item3Price" from="${1..10}"/>
And Output Should be stored in one field
ItemPrice: 2,8,6
Your question is a bit vague, so hopefully a somewhat vague answer will be helpful too!
If you have a domain object Foo:
class Foo {
String itemPrice
}
Then in your controller action, you can just do something like:
def save() {
Foo f = new Foo()
f.itemPrice = [params.item1Price, params.item2Price, params.item3Price].join(",")
f.save()
}
Really all you're trying to do is join your parameters from your page into one string, right?
Now this actually seems like bad design to me. What happens if the order changes, or if nothing is selected for item 2? Or what happens if somebody wants to edit your object and you need to parse the values back out? Obviously you could split on commas...until one of the values contains a comma!
You're better off storing one value for each field that means something different, or storing a single field as a structured value. You might want to look at encoding a Map into JSON and storing that, for example, if you really just want to have one field in your domain object.

How to update value in angular ui typeahead directive if no matching option is found

I've an array of objects containing title and salary which is used in typeahead directive.
I display department name and get entire object as value.
If none of the options match, I want user entered string to be converted to object still. Is there any way to update that?
This answer is pretty late, but I would just use ng-blur (to trap the end of the users input) along with a variable bound to typeahead-no-results. Then test if the variable is true in the method bound to ng-blur, and, if so, make an object out of the String supplied by the user and push it to your data source. Simple example found here.

Grails pass object to the view and back again

I have some data that I need to persist through multiple actions within my Grails app. Due to the nature of the data, I would prefer not to store the data in the session. Here is an example of what I would like to do.
class MyController{
def index(){
MyObject object = MyObject.new(params.first, params.second, params.third)
[gspObject:object]
}
def process(){
MyObject object = params.gspObject
//continue from here
}
}
In my GSP if I do
<g:form action="process" params="[gspObject:gspObject]">
Then I get the error
Cannot cast object 'net.package.MyObject#699c14d8' with class 'java.lang.String' to class 'net.package.MyObject'
My question is, If I want to get the object back that I sent to the gsp, how can I get that? Is there some kind of scope that I can save the object in that would be a little safer then session? Is there a way to pass the object into the page itself and pass it back in the next request?
Grails has many layers, but at the bottom you have plain old HTTP just like in any web app. It's a stateless protocol, and you send a text or binary response, and receive text or text + binary requests. But you can't expect to be able to send an arbitrary object to a web browser in HTML and receive it back again in the same state as when you sent it - where is this Java/Groovy JVM object going to be stored in the browser?
You have basically two options. One is to store it at the server, which is less work because it remains as the same object the whole time. The session is a good location because it's coupled to the user, is created on-demand and can automatically time out and be removed, etc. The other is to do what you're trying to do - send it to the client and receive it back - but you are going to have to serialize it from an object (which could be a complex object containing arbitrarily many other objects) and deserialize it from the format you used on the client back into Java/Groovy objects.
JSON is a good option for serialization/marshalling. You could store the stringified object in a hidden form element if your page uses a form, or in a querystring arg if you click a link from this page to the next in the workflow. Don't send all of the object's data though, only what you need to rebuild it. Anything that's available in the database should be referenced by id and reloaded.
Something like
[gspObject: object as JSON]
or
[gspObject: [first: object.first, first: object.firstsecond, ...] as JSON]
will get it in the correct format for sending, and then you can parse the JSON from the request to reinstantiate the instance.

Update only the changed values on Entity object

how can i automatically update my entity objects changed values and save them to db.
I hava an Action like that
public ActionResult Update()
{
User userToUpdate = new User();
TryUpdateModel<User>(userToUpdate,ValueProvider);
BaseRepository.Context.AttachTo("User",userToUpdate);
BaseRepository.Context.SaveChanges();
return Json("");
}
ValuProvider : has the items that come
from the client as post data.
The problem on this code is the code update all the values but i want to update only the changed values.
How can i find the changed values on my entity object.
You should check out the ObjectContext.ApplyPropertyChanges Method
it is suppose to do what your asking for...
msdn
Two options:
On the View you could know the values that were changed by using Javascript and then you could pass that information to your controller.
You could simply compare the previous values (which you already have since you populated a view) and check each value before updating the DB.
I prefer last option, since at this point you could also check for data validation.
This is really a problem for your data access code, not anything to do with your controller. Pick an ORM that handles this for you and forget about the problem.

Resources