Using Breezejs generate query string - javascript

I have a custom Odata endpoint which gets it filter via a post from the body and not the query string. At the moment all of the queries are hand coded and then fed in to the post.
I was wondering if anyone knows if I can use Breezejs just to create the query I need.
Cheers

According to the "OData AJAX" section in Controlling AJAX you'll need to make changes to your copy of data.js so that POST is used instead of GET.
If you don't care to have breeze load the data and just want to use the EntityQuery syntax to build queries you could try getting an instance of the breeze odata uriBuilder and call the buildUri method to convert the EntityQuery to an odata uri. Assuming that works, it would be a matter of grabbing the querystring component of the uri to use in your POST body.
var interfaceRegistry = breeze.config.interfaceRegistry,
uriBuilderInterface = interfaceRegistry.uriBuilder,
uriBuilderCtor = uriBuilderInterface.getImpl('odata').ctor,
uriBuilder = new uriBuilderCtor(),
uri = uriBuilder.buildUri(entityQuery, metadataStore);
There might be a better/simpler way of doing this... maybe by grabbing the uriBuilder from a breeze odata dataService instance. I don't think this is part of breeze's supported/documented public api but it sounds like it would make a nice feature suggestion to post on the user voice site.

Related

ember adapter pass id

I want to be able to pass an id to rest point while using ember data. My end-point looks like v3/enterprise/inventory/items/{id}/links. I want to inject the id while making the request such as this.store.findAll('each-item-links', { id: itemId }). However, it does not work. I extended the Ember REST adapter and override the namespace but nothing seems to be working.
If you're trying to request a single record through Ember Data, then you want to use findRecord instead of findAll.
Also, if you need control over how the URL is built (what you have there looks like it might not map to the RESTAdapter too cleanly) you can override the _buildURL method to change the URL that the request is sent to. It is given the ID from findRecord so you can generate the URL whatever you want. Technically this is "private API" but I wouldn't worry too much about overwriting that.
Edit: To avoid using private API, there is also a public buildURL method that can be used instead.

How can I get a SpreadSheet Google using XHR?

I'm trying to get Spreadsheet data Google. So, to do this, I've seen the Query Language Reference and basic information about Query object and, in this second link, it appears the "fast" info to realize a query. The problem is that, because of my JS skills, I don't understand opt_options parameter of the google.visualization.Query function.
I mean, I know that I have to send my query to get the data, but I don't know how to do it because the sendMethod description of opt_parameters doesn't seems me sufficiently detailed for write the JS code whether I want use the XHR method.
Also I've seen that I can include the query in the SpreadSheet link. Anyways, I only want to get the data transformed into JSON data or something that I can manipulate.
I consider that this question can be useful for this people like me that are beginning to get data from other pages and don't know how to do it exactly.
PD: Yes, I've read XMLHttpRequest info from MDN.
EDIT: I've supposed that Query object send XHR and I only have to concern me for send method of Query and its callback that is an instance of QueryResponse class. The problem is that I don't retrieve anything! Here is my code once again:
var query;
function getData(DATA_SOURCE_URL){
query = new google.visualization.Query(DATA_SOURCE_URL);
query.setTimeout(10);
query.send(displayData);
console.log("Query has been sent");
}
function displayData(response){ //This function doesn't works
if(response.isError()){
console.log(response.getDetailedMessage());
}
var data = response.getDataTable();
alert(data);
}
Thanks in advance!
(Posted answer on behalf of the OP).
Finally I got rid of Query Language Reference and all stuff about Google and I discovered TableTop to request data from spreadsheets. It's very easy to use and its documentation is simple and concise. My main aim was bring data from a spreadsheet to my JS application.

Json parameter casting inheritance web api 2

I am using MS Web API 2 to receive calls from our web page using ajax.
Then I have 2 classes: subscriber and externalSubscriber. Subscriber contains very basic data like a name and id. External subscriber inherits from subscriber and adds basic data like address and email. I have one api method to edit the data of a subscriber defined like this:
public IHttpActionResult PutSubscriber(int id, Subscriber subscriber)
In our page I create a json string using the data provided which leads to it ether being a external or a normal subscriber.
I am able to post to this function using both but with an externalSubscriber object the added data gets lost and trying to cast from subscriber leads to an error.
my question is if anyone has any experience with this issue and if there is another way to fix this besides creating a specific function for putting a external subscriber.
I have got it to work by using the information provided on the following pages:
http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm
Polymorphism in Web API: Single endpoint possible?
First I set the json setting:
jsonsettings.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
then setting the type of the json object before sending with ajax using
jsonObject { '$type' : .Models.ExternalSubscriber, solution.project', ....}
after this I am able to cast the incoming Subscriber to an ExternalSubscriber and access its properties.

How can I serialize backbone model into URL

I want to store state of a simple application in a model, and be able to generate URL containing full state of the application (i.e., serialized model). Is there a backbone-idiomatic way to provide such a feature?
Related:
BackboneJS model.url using collection.url
Backbone.js state management / view initialization based on url fragment
Backbone let's you serialize models to JSON but not to URL parameter syntax. You could stringify the JSON, url encode it, and then add it to the uRL ...
var json = yourModel.toJSON();
var jsonString = JSON.stringify(json);
var encoded = encodeURI(jsonString);
var url = 'www.example.com?model_json=' + jsonString;
... but I wouldn't recommend it. URL parameters were never designed to be used that way, and you can run in to lots of problems. One problem is character length limit. As this Stack Overflow answer explains, urls need to be limited to 2000 characters to be safe. Depending on what you put in your model, you could certainly exceed that without meaning to.
A much better approach would be to save the model somewhere that's designed to hold it, like on the server (that's exactly what the save method of a Backbone.Model is for, after all) or in local storage. Then, all you need to put in the URL is the model's ID, and you can use that ID to retrieve the data remotely/from local storage.

Breeze.js: How to include all related data in payload even if it was sent

I have a list of of data being return from a "standard" HttpGet IQueryable method from an ApiController that implements the Breeze EFContextProvider. When one of the objects references another object that has already been returned in the payload, Breeze gives me an $ref to refer to the object that was already returned.
I want the object with all related objects return explicitly, not a reference with $ref. Also, I'm not using the breeze.js library on the client side; simply making straight calls to the Controller with a web address.
I found this:
Breeze does not replace the Ref: node with its real data
which is the thing I'm looking for, but using Include on the server still doesn't return all of the data.
Any idea on how to "force" Breeze on the server side to include all related data no matter if it was returned and referenced in the payload?
Update 1
Per Steve's answer below I added the following to the BreezeWebApiConfig.RegisterBreezePreStart method in the App_Start folder:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Object;
Compiling and running produces the same output with only the $ref group instead of the full data. I'm sending a request to the server to $expand the collection. Do I need to change the SerializerSettings upon each request to the controller or will adding this to the BreezeWebApiConfig.RegisterBreezePreStart method be enough?
Update 2
I've added a CustomBreezeConfig class per the instructions at the link that Steve added in his answer. I am however using Breeze.WebApi2 so it appears that the BreezeConfig is actually in Breeze.ContextProvider. The code compiles, but I'm still seeing the same $ref for the actual object in JSON.
Do I need to include this CustomerBreezeConfig class in a specific place in my project for Breeze to use it's serializer settings?
Under WebAPI, Breeze uses the Json.NET serializer to turn the results to JSON. You can change the serializer settings (specifically the PreserveObjectReferences setting) to change this behavior.
Breeze configures it's own JSON serializer, so in a Breeze app, you'll need to configure it as described in the Breeze Web API Controller doc.
Note that, if you turn PreserveObjectReferences off, you might also need to configure the ReferenceLoopHandling setting, if you have circular reference in your object graphs (as most of us do).

Categories

Resources