angularjs $resource.query with complex structure - angularjs

all my rest API for the query (e.g. /clubs) return an object that is of this form:
{
"total": 10,
"results": [...]
}
the $resourece.query expect an array.
what's the correct way to parse this response?
Should I override in the definition of the resource the query setting isArray: false? (if I set the functions to override the query do I've to re-set all the other methods such as get save etc?)
Should I create a parseFunction to parse the result and return an array? if so, how?
how can I create a my resource in a way that I don't have to replicate the code for every resource? basically extending the current $resource object to override the configuration, or the parsing function.

What are you receiving back is an object and not an array, so use $resource.get, then you can access the results array from the object

Related

Get parameter from a given URL (not the calling) in Logic App

I have an URI as a string which I get out of a Json in my Logic App.
How can I access any of the query items of the uri?
Inside the For Each which loops through the uri string list, i tried the following expression to get the query parameter filename, but it did not work:
items('For_each_2')['queries']['filename']
This returns 'The template language expression 'items('For_each_2')['queries']['filename']' cannot be evaluated because property 'queries' cannot be selected'.
There is a URI parsing functions in logic app to query property from URI, suppose you want is uriQuery, however it returns the whole after ?. It will be like below.
And if you want to query a specific property, you should pass them with the logic app trigger URL. It provides triggerOutputs()['queries'] property to get the queries, and this will return a json object, it will allow you to query specific parameter.
This is a sample:
https://xyz.logic.azure.com:443/workflows/id/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=code&test=123&test2=345
Then should be able to use triggerOutputs()['queries']['test'] to query.

sending qooxdoo form controller model through RPC call to the server

I have form controller and send its model to the server via JSON RPC - it comes there nicely but with a lot of internal fields:
"0" : "Please use 'toArray()' to see the content.",
"$$hash" : "427-0",
"__objectHash" : "427-0",
Is there a correct "qooxdoo" way to create JSON object from model with just user properties?
Thanks!
If you need to serialize your model to JSON you can use qx.util.Serializer.toJson(). It will convert your model to JSON containing only the model's properties. You can even pass to it your own serializer function, if you wish, to customize the serialization according to your needs.
The Demo Browser has an example on how to use qx.util.Serializer.toUriParameter() that does exactly the same thing but returns an URI parameter instead of JSON.

How to prevent hard-coding in angular application

I am currently working on a angular project where I find myself injecting hard-coded values of string, numbers, etc into html template.I have tried to store hard-coded values in object fields and inject the object into whichever component needs it. However it does not seem very intuitive. Is there way to get values from some properties file like we do in Java. I would also like to know what are some of the best practices that you developers are using to prevent that?
You can do it with a properties file as well. The best way would be to create a json file, e.g., example.json and calling in the json and then just making an API call like:
$http.get('your_web_address/example.json')
.success(function(data){
// assign the data to a var or write next logic directly
})
and then use the content of json as per choice
You can make this as a service call as well, if you want

ng-repeat convert resource objects to simple objects

Iam getting my data with help of the Angular's $resource service as array. Each element of that array is an Resource-Object. So i can use methods like $save and $update of these Objects. In a view i represent my array with the help of the ng-repeat directive like:
<div ng-repeat="appointment in object.appointments" ng-click="editAppointment(appointment)">
And here i get in trouble. The appointment-Object i get in the editAppointment-Method is a simple Object. No Resource Object anymore. So i cant use the helpfull methods like i mentioned above.
$scope.editAppointment= function(appointment){
console.log(appointment); // > Object
console.log(object.appointments); // > Array of Resource
}
Have somebody noticed that problem too? May its a bug, but i cant imagine that.
Assuming your resource class is called Appointment, you should just be able to do:
$scope.editAppointment= function(appointment){
new Appointment(appointment).save();
}
Presumably your Appointment resource looks something like the following (i.e. it correctly maps some sort of id property from existing objects to the URL parameters):
var Appointment = $resource('/appointment/:appointmentId', {appointmentId:'#id'});
This would be the case if your appointment objects (i.e. the underlying JSON objects handled by your API) have an ID property called id. If it's called something else (or if there are multiple path variables in your URL) you'll just need to change the second argument to map all of the properties of the objects being saved (the values starting with '#') to the URL path variables (the things starting with ':' in your URL).
See where they save a new card in the credit card example here: http://docs.angularjs.org/api/ngResource.$resource. The fact that they're dealing with a totally new object and that you're trying to save an existing one is irrelevant. Angular doesn't know the difference.

backbone.js model and collection overhead

When I fetch models or collections from the server, I am not able to access properties of the model unless I stringify then re-parse. Presumably the models themselves have some extra overhead from backbone.js? Note that in the below code I can perform stringify/parse sequentially, which is supposed to give the same result as I started with. However, clearly I have killed off some superfluous info by performing these two steps because my model's properties are now exposed differently from before. Surely I do not need to go through these two steps to access my model properties, right?
Eg.
thismodel = /// assume this came from server fetch
alert(thismodel.name); // DOES NOT WORK - undefined
jsonmodel = JSON.stringify(thismodel);
var providerprefslistJSON = jQuery.parseJSON(jsonmodel);
alert(providerprefslistJSON.name); // WORKS
Backbone Model objects are not plain old JavaScript objects. They keep their attributes in an internal hash. To access the name attribute you can either do this:
alert(thismodel.attributes.name);
Or better yet use the get() method:
alert(thismodel.get("name"));
The reason it works when you convert the model to JSON and then back again is because JSON.stringify calls the toJSON() method, which creates a JSON string from the internal attributes hash, meaning when you parse that string you get a plain old JavaScript object - which is not the same as a Backbone Model object.
First, are you trying to access the property of the model or response?
From alert(thismodel.name) it would seem that you're going for a property of the model not the attribute. If you're looking for the model attribute then perhaps you want alert(this.model.get('name'))
If you're indeed going for model.name, then basically the problem may lie in how you're parsing the data. Say for example the JSON from your server is like this {'name':'Jimmy'}.
While the model.response the raw JSON sent has "Jimmy" namespaced under object.name, Backbone will automatically take that and turn it into a model attribute unless instructed otherwise (e.g. modelObj.attributes.name) at which point you'd use the get() function.
You should be able to access model data fairly simply if everything works.
E.g. Fetch
var model = new MyModel();
model.id = 1;
model.fetch({
success: function(model, response) {
console.log(model.get('name')); // The model name attribute
console.log(response.name); // The RAW response name property
}
});
Or maybe your server isn't sending the data back as JSON data. Is the server response content-type="application/json" ?
Some things to check.

Resources