sending qooxdoo form controller model through RPC call to the server - qooxdoo

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.

Related

How to get tabs in angular forms based on given xml file?

I have to get number of input tabs or fields based on given xml or json file.
example : If xml has 3 tags, then form should contain 3 input fields.
Same with JSON.
You can use ng-repeat on service response array of objects. And also you can create new scope variable & assign unique property of response array object to it. Then you can have as many number of input fields based on service data & also dynamic ng-model on them to handle their model data to submit it.
This is small example which demonstrates your requirement: http://jsfiddle.net/DrQ77/
For xml response, better use xml to json library plugins to convert it to json data https://github.com/abdmob/x2js. Or prefer server side sending json data (with implementing xml to json functionality at server side).

What is the best way of querying data with JSON object (through MongoDB) from AngularJS

I have generated a Mean stack app with the angular fullstack generator. In the generated parts of the project, querying data from angular controllers is achieved through a REST api. A typical get endpoint looks like this:
GET /api/things
And this api can be used directly from controller like this:
$http.get('/api/things', { params : {'name': 'xxx'}})
With this approach, i can query data just with string parameters. When i want to query data using a JSON object like this one {'age': {"$gte": 18}}, it does not work since get only support strings as parameters. Now i am not sure what is the best way of querying data with JSON objects. I now have these options in my mind, but i am not sure which may work, or which way i should choose:
1- $http.get('/api/things', { params : encodeURIComponent(JSON.stringify{'age': {"$gte": 18}})})
using this encoding i can pass the JSON object as string. But on the server side i am not sure how i can handle this string. req.query or JSON.parse(req.query) did not work.. This cannot be the right thing to do anyway, since it looks like a hack.
2- Creating another GET api like this:
/api/things/older
calling this api:
$http.get('/api/things/older', { params : {'age': '18'}})
and at the server side implementing the Things.find({'age': {"$gte": req.query.age}})
This is of course not a REST api then.. I don't know if it works at all
3- I have seen some projects where some factories/services are implemented (i have not used one yet). They are injected to controllers, and can be used like this:
var things = Things.query();
However i also don't know whether what i want can be achieved with this approach or this is the right way to do, since as i heard we should only be using our REST api(?).
So the question: What is the best way of querying data with JSON object (through MongoDB) from AngularJS?
Edit: For the 1. option: when i pack my encoded object in another key like this:
$http.get('/api/things', { params : {myQuery : encodeURIComponent(JSON.stringify({'date': {"$gte": new Date().setHours(0,0,0,0)}}))}})
and unpack on server like this:
Thing.find(JSON.parse(decodeURIComponent(req.query.myQuery)))
it works. But it somehow still does not sound right to me.

How to expose User.id in JSON response from Loopback

I have a simple use case that for the life of me I can't seem to figure out:
I have a model "Employee" that is based on the Loopback "User" model. My assumption is that the "id" of "Employee" would be the same as "User".
For the purpose of passing an Employee/User.id to an Angular.js route, I need to expose the Employee model's 'id' property in the RESTful response of the my Loopback API. Any idea on where or how to do this?
So far the Employee.id is profiled with the string "objectid"--which of course is not going to give me the correct user when I perform a Employee.find().
User.id is provided by LoopBack out-of-box. You can test this by adding a dummy user (via a boot script) and then browsing to localhost:3000/explorer to inspect the output of any GET request on the User object.
So, upon a more thorough review of the actual data in the Mongodb database it appears that '_id' in both User and Employee tables were always being populated with "objectid". This made me look at my models more closely. I compared them to the loopback-angular-admin's models--specifically their custom "user" model. What I noticed was that I had '"idInjection": true' on mine whereas the loopback-angular-admin project did not. So, I removed this property and VOILA: BSON/valid ObjectIds!
My models were generated with 'slc loopback:model'. This command line appears to add the '"idInjection":true' property to the models it generates. I don't know why this particular property would be problematic for Mongodb but the source of my original issue has been resolved.

Get View Output In CakePHP 2.x Model

Context: I'm generating a PDF in a model callback (afterSave). I found a library I'm comfortable with called FPDF. I'm adding the FPDF class as a vendor.
So, without going into too much detail, essentially, once all checks have been completed for a particular contract application, the app needs to prepopulate a PDF file and attach it to an email.
I can figure everything out except how to generate the PDF in the model. I want to use a view to pass view vars to so that I can populate the template file and use the FPDF class to save a PDF file.
This file will in turn be attached the automated email and sent to the applicant.
So the flow is:
Once all checks have been complete (via crons and a CakePHP Shell), we trigger a function inside the model afterSave callback.
The function performs some logic and determines whether a declined or approved email should be sent.
If approved, then a PDF is generated using a view file and saved in /Views/pdf/
This file is attached to CakeEmail object and sent.
It's just the view rendering part here that I'm stuck with:
3. If approved, then a PDF is generated using a view file and saved in /Views/pdf/
How can I populate a view file with view variables and return the result into a variable?
For example, think of how the CakeEmail class does it with the CakeEmail->template('example') function......
Any ideas?
The answer is to construct your view class manually:
$view = new View(null, false);
$view->set(compact('variable1', 'variable2'));
$view->viewPath = 'ViewFolder';
$output = $view->render('view_file', 'layout');

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