ember-data - best strategy for loading complex data structures - ajax

I am using ember-data 2.0. I want to load all data from the server at startup. Then the app is really fast after the initial loading. The application is mobile, and may have varying bandwidth. I want to load the data as fast as possible. I want to use JSON API to comply with the newest ways of using ember-data.
Each object is small, just a handful of attributes. But there may be 200 such objects to load.
Is it best to do many small ajax calls or one big one? I imagine it is faster to use one big call that contains all my objects, but is that possible with the JSON API and the out-of-the-box adapters?
Of course, I could just use findAll() but that will only load all objects of one type. I have several object types in my model that are connected with hasMany and belongsTo relationships.
If I use the RestAdapter out of the box, that will result in many ajax calls, right?
What is the best strategy to use and how to implement that with ember-data and the adapters?

You can load different types to the store from single payload out of the box with Ember Data. By default JSON serializer assumes that each root node in payload corresponds to a type.
Let's say you have models:
/models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
body: DS.attr('string'),
comments: DS.hasMany('comment');
});
/models/comment.js
import DS from 'ember-data';
export default DS.Model.extend({
comment: DS.attr('string'),
post: DS.belongsTo('post');
});
And you have an endpoint /posts which responds with payload like this:
{
"posts": [
{
"id": 1,
"title": "Post 1",
"body": "Post 1 body",
"comment_ids": [1, 2]
},
{
"id": 2,
"title": "Post 2",
"body": "Post 2 body",
"comment_ids": [3]
}
],
"comments": [
{
"id": 1,
"comment": "Comment 1",
"post_id": 1
},
{
"id": 2,
"comment": "Comment 2",
"post_id": 1
},
{
"id": 3,
"comment": "Comment 3",
"post_id": 2
}
]
}
So when you do store.findAll('post'), Ember Data will request /posts endpoint, recieve the payload and push both posts and comments to the store. And all relations will be resolved properly.
But there are some notes:
if you define model relation with {async: true}, Ember data will always hit the server to fetch relation records.
Everywhere in the app you would need to use store.peek() and store.peekAll() instead of store.find() and store.findAll() in order to get records from the store but not from server.

Related

how can I compose a bot to iterate trough a xml json object?

I am using the composer to publish a bot to fetch data from an azure storage table.
In short, the bot composer needs to construct a bot to iterate through an XML deserialized JSON object returned by the azure storage rest API.
In my code generated by the composer, the bot does a "set property" step immediately following the successful return of the REST API (storage table query). Given the deserialized object returned by the storage REST API, how should the "set property" statement be constructed so the bot can print our the individual data field,
Another way to phrase the question: how can I use the composer to construct the bot to iterate through a returned deserialized object (coded in XML JSON format)?
Where can I find a document that can shed some light on this matter?
Is there any place I can find a good example? Can it be done via composer?
Thanks in advance.
Yes, it can be done. If the API returns XML, make sure you configure your api call to ask for content type application/xml.
Then you can use use the xPath built in function. Make note that it will return an array if results in more than value matches the expression, in which you can use the foreach function to iterate over it with. I needed to run the nightly build of Composer (with bot-builder 4.12.0) to get it to work for me. See here for some more info:
https://github.com/microsoft/botbuilder-js/pull/3093
Here's an example that worked for me:
"actions": [
{
"$kind": "Microsoft.SendActivity",
"$designer": {
"id": "rGv7XC"
},
"activity": "${SendActivity_rGv7XC()}"
},
{
"$kind": "Microsoft.HttpRequest",
"$designer": {
"id": "TDA1wO"
},
"method": "GET",
"url": "http://www.geoplugin.net/xml.gp?ip=157.54.54.128",
"resultProperty": "dialog.api_response",
"contentType": "application/xml"
},
{
"$kind": "Microsoft.SetProperty",
"$designer": {
"id": "ipNhfY"
},
"property": "dialog.timezone",
"value": "=xPath(dialog.api_response.content,'/geoPlugin/geoplugin_timezone/text()')"
},
{
"$kind": "Microsoft.SendActivity",
"$designer": {
"id": "DxohEx"
},
"activity": "${SendActivity_DxohEx()}"
}
]
You can (if needed/you wish) use the json and jPath built in functions to convert xml to json and then query with. Something like:
${json(user.testXml)} and then
${jPath(user.testJson , "automobiles")}

How to remove hypermedia elements from representations produced by Spring Data REST?

When using Spring Data for my REST API, the responses returned currently include the _links field:
{
"_embedded": {
"users": [
{
"imageUrl": "some_image_url",
"name": "some name",
"id": "57420b2a0d31bb6cef4ee8e9",
"_links": {
"self": {
"href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9"
},
"user": {
"href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9{?projection}",
"templated": true
}
}
},
...
Is there a way to produce output, such that the _links field is hidden? e.g.:
{
"_embedded": {
"users": [
{
"imageUrl": "some_image_url",
"name": "some name",
"id": "57420b2a0d31bb6cef4ee8e9",
},
...
I find that because I am exposing the id field, _links are not really necessary, and mostly just clutter up my responses.
There isn't. Hypermedia is a fundamental trait of REST APIs and Spring Data REST heavily uses it to allow you to build clients that can use the links present in the responses to navigate to related resources.
Of course you can dumb down your clients to not make use of that information but that will lead to a much tighter coupling (as you can't change the URIs on the server side anymore, your clients expects to talk to a dedicated server whereas with hypermedia you can just point it to a different server etc.).
In contrast to a lot of other self-proclaimed REST frameworks out there, one of the key aspects of the framework's design is to respect the fundamental principles in REST and explicitly leverage them. Or at least, don't create incentives to easily break them. This is clearly expressed in the reference documentation and on the project website. Find out more about key design decisions in this presentation on Spring Data REST, and this one on Domain-Driven Design & REST.

What services can I use to fetch an image of a person based on their email address, without authentication

I know gravatar allows this, and I'd like to look into facebook, google plus, twitter, instagram, and anything else.
Do you guys know of any services like this?
EDIT: to clarify, by "without authentication" do I mean user authentication, I expect to use some sort of API
One possible approach, using Facebook Graph Api would be to perform a search against the name for users objects and also retrieve their pictures, using API version 2.2 it would be a call like:
GET graph.facebook.com
/search?q=miles davis&type=user&fields=id,name,picture
This would retrieve id, name and picture object for a search on miles davis, first results as example:
{
"data": [
{
"id": "805353876185303",
"name": "Miles Davis",
"picture": {
"data": {
"is_silhouette": true,
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-1/c15.0.50.50/p50x50/10354686_10150004552801856_220367501106153455_n.jpg?oh=XXXXXXXXXXXXXXXX&oe=XXXXXX&__gda__=XXXXXXX_XXXXXXXXXXXXX"
}
}
},
{
"id": "1572370603017419",
"name": "Miles Davis",
"picture": {
"data": {
"is_silhouette": false,
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p50x50/1501761_1382020035385811_1384168153_n.jpg?oh=XXXXXXXXX&oe=XXXXXXXXXXX&__gda__=XXXXXX_XXXXXX"
}
}
}, ...........
The field is_silhouette is false if the user has a profile picure.

RestKit 2.0 - Mapping json array to an enity relationship loses array sequence

I have a problem mapping json to CoreData and reading it out again. I map from json to an Activity-Entity with a relationship of last participant entities. The last_particpants is an array with the most recent participants, ordered from most recent first by the API.
{
"id": 50,
"type": "Initiative",
"last_participants": [
{
"id": 15,
"first_name": "Chris",
},
{
"id": 3,
"first_name": "Mary",
},
{
"id": 213,
"first_name": "Dany",
}
]
}
I have RestKit logging on and see that the mapping reads the array elements one by one and keeps the order. However CoreData saves them as an NSSet of entities and then the order gets lost. When I read out the data its is mixed up. What options do I have to keep the order in which the array was mapped? Any help would be great.
2 options:
Use an ordered set in Core Data (set on the attribute in the properties inspector).
Use the #metadata provided by RestKit to access the collection order during mapping.

datatables server side response nesting

Simple issue regarding usage of datatables (http://www.datatables.net) and server side data retrieval.
Datatables expects a special format from the server, to be in the root of the response:
{
"sEcho": 1,
"iTotalRecords": "57",
"iTotalDisplayRecords": "57",
"aaData": [
{
"engine": "Gecko",
"browser": "Firefox 1.0",
"platform": "Win 98+ / OSX.2+",
"version": "1.7",
"grade": "A"
},...
]
}
But I want to nest all of this in another place in my json response from the server:
{
myshit: "",
status: "success",
morestuff: "yoyo",
datatables: { here will be all of the json related to datatables }
}
I haven't found a property for this in the API.
The only options I know about, right now, is to define all the columns with the exact nested property they should look for.
Do you know of such a property? or can suggest a good way to achieve this nesting?
Because sAjaxSource expects the dataset in the particular order, you would have to use $.getJSON instead to have the nested layout you described

Resources