I'm currently handing down JSON data from Rail in the DOM. It looks like this:
> gon.posts
[
{ id: 1, name: "Post 1", related_post_ids: [3] },
{ id: 2, name: "Post 2", related_post_ids: [2, 3] },
{ id: 3, name: "Post 3", related_post_ids: [1, 2] }
]
How would I be able to write some AngularJS (I'm ruby) so that I could execute this pseudocode:
// Using "Post 1" and mapping to its related posts.
> gon.posts[0].related_posts
[
{ id: 3, name: "Post 3", related_post_ids: [1, 2] }
]
// Then being able to do this recursively to make development with the JSON easier:
> gon.posts[0].related_posts[0].related_posts
[
{ id: 1, name: "Post 1", related_post_ids: [3] },
{ id: 2, name: "Post 2", related_post_ids: [2, 3] }
]
It would also be helpful if you could show how to do this via a map
or each
function with AngularJS.
A user commented that Array.prototype.filter()
would be useful, but I'm not looking for a gross solution, I'm looking for the best, minimal, DRYest solution via AngularJS.
Ember Data has a find()
method. If I was able to use Ember and load the data up, I would be able to easily search, but I would still need something like an ActiveRecord::Proxy
object in JS to be able to handle the delegations and I'm not sure how to meta program new object types in JS.