Is there a way to display multiple collections on a single page using Backbone.js and Rails 3? - ruby-on-rails-3

I am trying to develop a single page application using Rails 3 and backbone.js. Is there away to instantiate two different collections in a router? Or would I need two routers routing to the same route?(I don't think this is possible)
I have two collections and I don't see why this is so hard to figure out. Any ideas?

You can instantiate and fetch as many collections, models, or views you like in a single route.
function showBooksAndCars() {
var carsCollection = new App.collections.Cars();
var booksCollection = new App.collections.Books();
carsCollection.fetch();
booksCollection.fetch();
var carsView = new App.views.CarsView({collection: carsCollection}).render();
var booksView = new App.views.BooksView({collection: booksCollection}).render();
$('div.cars').replaceWith(carsView.el);
$('div.books').replaceWith(booksView.el);
}

Related

How to retrieve the Id of a newly Posted entity using ASP.Net Core?

I Post a new Waste entity using the following code:
var result = await httpClient.PostAsJsonAsync(wasteApiRoute, waste);
The Api Controller (using the code created by VS) seems to try to make life easy for me by sending back the new Id of the Waste entity using:
return CreatedAtAction("GetWaste", new { id = waste.Id }, waste);
So the resultvariable wil contain this data. Indeed, I find it in its Headers.Location property as an url.
But how do I nicely extract the Id property from the result without resorting to regular expressions and the like? Surely the creators of ASP.Net Core will have included a nifty call for that?
Well, the best I can come up with is:
var result = await httpClient.PostAsJsonAsync(wasteApiRoute, waste);
var newWaste = await result.Content.ReadFromJsonAsync<Waste>();
Where waste has an Id of zero, newWaste has its Id set.

Categories list in Piranha CMS?

I'm using Piranha Core 8 with Aspnetcore 3. So far everything is going well. (I love Piranha!)
My current problem is when building a Sidebar with a list of categories.
I can't figure out how to retrieve all categories from the Api.
So far, I can get a list of posts, and perhaps iterate over them to collect the categories but this seems inefficient.
Any one know how to retrieve a list of all the categories from the cshtml pages?
You can get the full list of taxonomies per Archive by calling the Api.
var categories = await api.GetAllCategoriesAsync(archiveId);
var tags = await api.GetAllTagsAsync(archiveId);
Best regards
From the razor page I got it working this way :
#{
var archiveId = WebApp.CurrentPost == null ? WebApp.CurrentPage.Id : WebApp.CurrentPost.BlogId;
var categories = await WebApp.Api.Posts.GetAllCategoriesAsync(archiveId);
var tags = await WebApp.Api.Posts.GetAllTagsAsync(archiveId);
}

How to search multiple api services

I am developing a search engine with angular 2.
Therefore I use APIs from multiple platforms.
It works if I call the search function from every api service manually.
But is it possible to do the same foreach api service?
Every api service has the same function:
search (query: string): Observable<Array<SearchResult>> { ... }
In the UI I want to separate the results by tabs.
Therefore every api service has a title:
public title: string = "the title";
For storing the search results locally I have a class that is extended by every api service. This class has helper functions etc.
Depending on the behaviour you need you can use merge, concat or forkJoin to merge multiple streams into one.
The code would look pretty much the same.
For example using merge in order to merge 2 streams into one.
If you have a list of apis you need to call for the search. Your code would look like this.
let apis: string[] = [];
let observables = apis.map(api => search(api)); // get an array of observables
let merged = observables.reduce((previous, current) => previous.merge(current), new EmptyObservable()); // merge all obserbable in the list into one.
merged.subscribe(res => doSomething(res));
This article might be helpful.

Define a predecessor when using Rally WSAPI to add user story

I'm working on a .NET application to add user stories to our Rally workspace, and I'd like to set one of the user stories as a predecessor to the next one. I can add the stories just fine, but the predecessor/successor relationship isn't being created. I'm not getting any errors, it's just not creating the predecessor. (I'm using the Rally.RestApi .NET library).
I have the _ref value for the first story, and I've tried setting the "Predecessors" property on the DynamicJsonObject to that.
followUpStory["Predecessors"] = firstStoryRef;
I also tried creating a string array, no luck.
followUpStory["Predecessors"] = new string[] { firstStoryRef };
I kept the code examples to a minimum since the stories are being created fine and this is the only issue, but let me know if sharing more would be helpful.
The easiest way is to use the AddToCollection method. Check out the docs:
http://rallytools.github.io/RallyRestToolkitFor.NET/html/efed9f73-559a-3ef8-5cd7-e3039040c87d.htm
So, something like this:
DynamicJsonObject firstStory = new DynamicJsonObject();
firstStory["_ref"] = firstStoryRef;
List<DynamicJsonObject> predecessors = new List<DynamicJsonObject>() { firstStory};
OperationResult updateResult = restApi.AddToCollection(followUpStoryRef, "Predecessors", predecessors);

Serving local, pregenerated JSON to my javascript passenger, rails

I am pre-generating data points for graphing with highcharts.js, and storing them in my public folder. I seem to be having issues in passing them efficiently to my JS code, so right now, I'm reading them in my view and pasting them into the function call within the HTML. This doesn't feel right. Ideally, I think I should be serving them from Apache, AJAX style, to my JS function, but the current method is what seemed to work...
generate_plot.js:
var generate_plot = function(str) {
var t_test = str[3];
var kdes = str[2];
var new_vals = str[0];
var old_vals = str[1];
var chart_number = str[4] + 1;
....
In my view (haml) I'm doing:
...
- txt = ['public', (comparison_path(params[:id]) + graph).split('/').uniq.delete_if{|a| a.empty? }].join('/')
%div.bean{:id => "bean#{i+1}" }
%div.time{:id => "time#{i+1}" }
:javascript
generate_plot(#{File.readlines(txt).first[0...-1] + ','+ i.to_s + ']'})
This ends up generating some pretty clunky and atrocious html. I know there is a better way!
What is the proper way to serve local JSON data to the client for rendering? I was trying to do this before, but my server didn't find the local JSON files directly, so I started using the Rails app to read them in.
Is there a reason my server wouldn't find the JSON? Should I use AJAX to speed it up, or should I push it into the HTML (is it equally fast?)
Any direction here would be helpful, as I'm not ever sure what to look for.
In case when you have two different servers, one with highcharts, second with JSON, probably you come across cross-domain problem. So you can use JSONP, which reduce it.