angularjs - pass param from controller to service - angularjs-service

controller.js
From here to factory I want to pass a param:
getSurveyDataDrag.query(function(data){});
Like something:
getSurveyDataDrag.query(param: "1", function(){})
service.js
Want to get to inside factory(below) param value 1:
.factory('getSurveyDataDrag', function($resource){
return $resource('/getSurveyData/:flag', {falg: '3'}, {
query: {method:'GET', params:{orderby1:''}, isArray:true}
});
})

I'm pretty new to angular too, and not being sure what exactly you are trying to do, I can at least offer this to hopefully help you along since no one else has yet :)
Instead of param: "1" in the following line:
getSurveyDataDrag.query(param: "1", function(){})
Try passing the parameter by name & value as an object literal, more like this:
getSurveyDataDrag.query({orderby: "whateverYouWantToOrderBy"}, function(response){
//do what you wanna do here
});
Angular typically expects parameters to be passed in the form of an object in my experience, and you don't have to identify that they are parameters when you are just passing them through.
Now I'm not sure that syntax is right - I've had trouble finding info on .query myself. If that doesn't work, it could be that because you are overriding the natural angular query, you need to say getSurveyDataDrag.$query (note the $).
I hope that helps a bit!

Related

elegant way to redirect with array in GrailsParameterMap groovy

Is there any elegant way to redirect with the array in GrailsParameterMap?
When I type:
redirect(action: "XXX", params: params)
I get array which looks like [Ljava.lang.String;#596a40f1
EDIT: I have more precisely defined my question.
'params' are accessible by ALL controllers/Interceptors when redirecting. The one thing to keep in mind is that a redirect issues a NEW request/response so the headers can be different from the original request you got...99% of the time they will NOT BE! But this is something to keep in mind.
Also if, you need 'params' in spring filter, you just have to manually parse the request yourself.
But as 'params' are merely the request parameters, they are available to all components that have access to the request. :)
Hope that answers the question.
to pass an array of values into one parameter you have to convert this array to ArrayList
example:
//String.split() returns as array of strings
//as List - converts array to a List
params=[cId: "1,16,18".split(',') as List]
you could convert params.cId parameter to a List just before redirect
params.cId = params.cId as List
redirect(action: "XXX", params: params)

Passing parameters to next view from Action method

I am looking out for different alternatives of passing parameter from an action method in JSF Managed bean to next view.
For example, I have an action method in my managed bean.
public String actionMethod01(){
String outcome = "nextPage";
return outcome;
}
If I want to pass a parameter to nextPage, one option which I have is:
outcome += "?param1=value1";
But, its not so convinient if I have multiple parameters to be passed.
Is there a better way for doing it?
Best regards,
Anand.
There's nothing in the JSF API which makes this easier. Just create a helper/utility method yourself which makes it more convenient so that you can end up with something like this:
return "nextPage" + toQueryString("param1", "value1", "param2", "value2");

Use dynamic url for Backbone Collection

Backbone configure url once for all when a Collection is created. Is there a way to change this url later?
The following sample shows 2 POST at /product and 2 POST at /product/id/stock. The last POST won't work, Backbone concatenate the id and try to PUT it, but I don't know why.
products.create({ name: 'American Pastoral', price: 8 });
products.create({ name: 'The Grapes of Wrath', price: 10 });
products.each(function(product) {
var id = parseInt(product.get('id'));
stocks.setId(id);
stocks.create({ id: id, quantity: 12 });
}
The stock collection:
Backbone.Collection.extend({
url: function() {
return this.url;
},
parse : function(resp) {
return resp.stock;
},
setProduct: function(id) {
this.url = '/product/'+id+'/stock';
}
});
This won't work.
Backbone.js will use the url of the model when saving existing models. Its not quite clear what you are trying to do -- I don't know what stocks is, for instance. Anyway, your code probably needs to look similar to the below and you should not be dynamically changing the url:
Product = Backbone.Model.extend({
url: function() {
return '/product/' + id + '/stock';
}
});
ProductList = Backbone.Collection.extend({
model: Product,
url: '/product'
}):
Backbone.js will then use collection url for creates and the model url for saves. I think you need to leave the url alone and let backbone's default functionality handle it.
I have run into what is essentially the same problem. It seems that Backbone's pattern is to lock down relative URI's in models and collections and allow the framework to use these to build final the final URI for a given resource. This is great for Restful URI's templates that don't change. But in a pure RESTful service you would expect lists of relative URI's to come down as part of a given resource. There are many reasons why you might need to do this, obvious one is if the URI for a resource moves.
As far as I can tell Backbone has no way of easily cleanly handling this. In the question above my workaround is to essentially redefine the models url method using OLN. You would do this while fetching a collection or initializing it for the first time. Basically build the logic to handle URI lists yourself.
I know this is an old question, but it took me a bit to determine how to solve it for my problem. In the manual under the fetch operation there is this tidbit:
jQuery.ajax options can also be passed directly as fetch options, so to fetch a specific page of a paginated collection: Documents.fetch({data: {page: 3}})
The object being passed to the data parameter is added as a URL GET variable. So URL would turn into URL?page=3 or URL?x=1&page=3 if there were existing variables already.

Action with a string array as parameter

I want to call an action with something similar to this uri:
http://server/controller/action/?columns=firstname&columns=lastname&columns=age
and use it like this:
public ActionResult Action(string[] columns)
{
}
how do I do it?
Google is my friend ;)
http://server/controller/action/?columns[]=firstname&columns[]=lastname&columns[]=age
Edit:
Actually you just write as I did in my original question. The reason to why I didn't get it working in the first place is that I used "column" in the query string and "columns" in as action parameter.
I don't know if it's the difference between get and post parameters, but your original post works perfectly good with post parameters. In fact, when using []'s in post parameters the array becomes null in the action parameter. I found this out when jQuery 1.4 started adding []'s in json arrays when posting. See: http://www.dovetailsoftware.com/blogs/kmiller/archive/2010/02/24/jquery-1-4-breaks-asp-net-mvc-actions-with-array-parameters

Controller parameter NULL when using jQuery POST and ASP.NET MVC

I have no problems processing jQuery GET requests in a controller, however I cannot get any form data to POST. The client snippet
$.post(url,{name:"John"},function(result){
//process result
});
combined with a controller,
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(string name)
{
return Json("Success!");
}
will result in a NULL value for the name parameter when inspected inside the action method, whereas I expected name to be mapped to the method parameter. Also all other objects (Request.Form), etc. in this context seem to be NULL. I can do this with a $.get, but I think I am supposed to do any operations with side-effects with POSTs. I am using ASP.NET MVC 1.0, jQuery 1.2.6 and Internet Explorer 7.
Thanks!
Update: see my answer below and humble apologies
Sorry guys, I had a $.ajaxSetup entry in the page which overrided the default contentType to application/json.
When using the default contentType as follows:
$.ajax({ url,
type: "POST",
contentType: "application/x-www-form-urlencoded",
success: function(result) { alert(result); },
data: { name: "John" }
});
It works because processData is true by default which means the data entry with the JSON object will be parsed into a string (data: "name=John" also works).
Sorry for wasting your time :) and thanks to Mark for the suggestion on passing JSON objects, ill do that next cause it seems very cool.
I believe your code should work, is your URL correct and your routes setup correctly? In addition you could always fire up Fiddler to see exactly what your request to the server is and if you are passing the correct items.
Could it be that the Save(string name) method is expecting stringified JSON? Try this:
$.post(url,
"{'name':'John'}", function(result){
});
It isn't so simple as making a json object and throwing it at an action.
Start from here. People have written small scripts that get the JSON object dressed and ready for an action to read it in and map it to properties or arguments.

Resources