I have a rather big Json object that I am receiving via $http that I want to share between 2 controllers. To give you an idea - both controllers share the parent most array (marked as levels), and then within the levels there are key:values with nested arrays ( 2 of them) and each controller uses and edits them separately. I am trying to consolidate this as much as possible so I thought if i could control 1 json between 2 controllers this would work.
I want to be able to edits each sub array and push back to the server as a whole, but before I attempt this step I would like to be sure something like this is even possible. I have tried using a factory and service to no avail. I am slo curious if I can do this and I bring in the array - is it 2 way binded? The first controller should be able to add more levels, and the second one can add to them in a differen't way the first one can't. If I were to bring in the json and share it between controllers and added a level on the first one, would it show on the second?
I have been banging my head against this for a while now, so any insight or direction would be very appreciated. Thanks!!
I took a stab at what the json might look like (forgive me if the syntax is off)-
{
"level1": {
"form": {
"form1": "sample1",
"form2": "sample2",
"form3": 500,
"form4": 500
}
"sublevels": {
"subL": ["1", "2", "3"],
"help1": "main_window",
"help2": 500
}},
"level2": {
"form": {
"form1": "sample1",
"form2": "sample2",
"form3": 500,
"form4": 500
}
"sublevels": {
"subL": ["1", "2", "3"],
"help1": "main_window",
"help2": 500
}
}
}
so both controllers would build off the levels, something like "level in levels" ng-repeat type scenario, but controller 1 would use the "Form" key, and controller 2 would use the "subevels" keys. they would be able to edit those sections and post the whole sting back to the server for a save. I don't have much else because the factories/services I Tried to setup didn't seem to work at all. Thanks!
you can put it on service, but a simpler way is use rootscope, or create a parent controller common for both of your controllers
Related
I am new to Ruby and have been stuck at this for a while now. I am getting a JSON responses as mentioned below and aim to search for the substring where the value of its substring is something as specified by me.
For example, I am getting the response below:
{
"00:00:00:CC:00:CC": {
"family": "lladdr"
},
"10.0.0.20": {
"family": "inet",
"prefixlen": "24",
"netmask": "255.255.255.0",
"broadcast": "10.0.0.255",
"scope": "Global"
},
"ff00::f00:00ff:fff0:00f0": {
"family": "inet6",
"prefixlen": "64",
"scope": "Link",
"tags": []
}
}
I need to get the value of the parent where the key family has a value equal to inet. In this case, I just want 10.0.0.20 as output when family equals inet.
I went through multiple questions here, and Google did not help. I understand that I will need to parse the JSON using JSON.parse, and then use maybe find or select to get my answer, but I was not able to get it working.
I am not sure if there is any other way I can do this like you would do in Bash using grep or awk. One hack might be to use something like foo.[46..54] which will output the IP, but again I believe that would be a bad way of solving this.
Use Hash#invert
Assuming that your Hash is already stored in response using JSON#parse, one way to solve the problem is to invert the Hash with the Hash#invert method. For example:
# Return an Array of IPv4, then pop the last/only String value.
response.invert.select { |h| h.values.include? 'inet' }.values.pop
#=> "10.0.0.20"
This is quick and simple, and it works with your provided data. However, there are some minor caveats.
Caveats
Assumes there is only one IPv4 address in the response Hash. If you have more than one key with inet as a value, don't use pop and deal with the resulting Array as you see fit. For example:
response.invert.select { |h| h.values.include? 'inet' }.values
#=> ["10.0.0.20"]
It assumes the key for each top-level JSON object is an IP address. We're not really validating anything.
It works for the JSON you have, but isn't solving for arbitrarily nested or varied data structures. If you have different kinds of inputs, consider it "some assembly required."
If you have no inet family, then {}.values.pop may return nil. Make sure you plan for that in your application.
None of these are show-stoppers for your particular use case, but they are certainly worth keeping in mind. Your mileage may vary.
If you want the family inet,
result = JSON.parse(response)
family = result.detect { |k,v| v['family'] == 'inet' }
family[0] # 10.0.0.20
Note that detect will return an array.
My app has two kind of objects, one is the location and the one is the route that goes from locationA to locationB.
location looks like this:
{
"idLocationX" : {
"address" : string
}
}
route looks like this:
{
"idLocationA": {
"idLocationB": {
"time": number,
"distance": number
}
}
}
I decided on that structure for route because initially I thought, I could just go ahead and access /routes/idLocationA/idLocationB but that was obvious shortsighted.
I know that Firebase advices against nesting objects but I feel it's still the right and best way to quickly access the route while still keeping the overall data structure as lean as possible.
The alternative would now be to change the route structure to something like this:
{
"idLocationA": true,
"idLocationB": true,
"start": "idLocationA",
"end": "idLocationB",
"time": number,
"distance": number
}
That structure is obviously a lot leaner but I would need to do a different query and it feels less natural.
Can someone explain to me if there is a way to get data structure 1 for route (the current one, firstly mentioned above) to work in a Firebase database setting? I would basically need to get the path /routes/{placeholder}/idLocationA/idLocationB working.
Firstly, I'm very new to nodeJS... like day 5. So bear with me.
I'm building an FAQ app to get into it and I have written a controller and a model which is returning this JSON file:
[
{
faq_id: 1,
faq_question: "This is a question?",
faq_answer: "And a lovely answer",
tab_title: "Tab title here",
tab_id: 1
},
{
faq_id: 2,
faq_question: "Another question",
faq_answer: "With an answer",
tab_title: "Tab title",
tab_id: 2
}
]
}
The requirements are that all the questions are categorised into tabs on the FE. So two tables, one for tabs and one for faqs.
Where each question has an appropriate tab_id associated with it to make the relationship.
What I need to do is create top level nesting in the JSON file to be all the tabs, then iterate over the questions and match the faq_tab_id with the tab_id and nest the question into the appropriate tab.
I have started with looking into using an UnderscoreJS map?
Not sure how much sense it makes or if any more code would be required, but any advice would be welcome. I'm not clued up on the terminology yet so I struggled to find an already asked question to do something like this.
Thanks!
Depending on what you intend to do, Underscore.js groupBy function may be enough:
// This will group the objects by their 'tab_id' property
var grouped = _.groupBy(myData, 'tab_id');
Hi so I'm new to CouchDB looks great so far, but really struggling with what must be simple to do!
I have documents structured as:
{
"_id" : "245431e914ce42e6b2fc6e09cb00184d",
"_rev": "3-2a69f0325962b93c149204aa3b1fa683",
"type": "student",
"studentID": "12345678",
"Name": "Test",
"group: "A"
}
And would like to access them them with queries such as http://couchIP/student?group=A or something like that. Are Views what I need here? I don't understand how to take the parameter from the query in the Map functions in Views. example:
function(doc,req) {
if(req.group==='A'){
emit(doc.id, doc.name);
}
}
Is my understanding of how Couch is working wrong or what's my problem here? Thanks in advance, I'm sure this is Couch 101
Already read through http://guide.couchdb.org/ but it didn't really answer the question!
You need views to achieve the desired results.
Define the following map function inside a view of a design document. ( let's name the view "byGroup" and assume this lives in a design document named "_design/students" )
function(doc) {
if(doc.group){
emit(doc.group,null);
}
}
Results can be obtained from the following url
http://couchIP:5984/dbname/_design/students/_view/byGroup?startkey="A"&endkey="A"&include_docs=true
To have friendly url couchdb also provides url rewriting options.
You need to some further reading about views and the relevance that they return key/pair values.
It's not clear what you want to return from the view so I'll guess. If you want to return the whole document you'd create a view like:
function (doc) { emit(doc.group, doc) };
This will emit the group name as a key which you can lookup against, the whole doc will be returned as the value when you look it up.
If you want to just have access to the names of those users you want to do something like:
function (doc) { emit(doc.group, doc.name) };
Your question arises from a misconception about what a view does. Views use map/reduce to generate a representation of your data. You have no control of the output of your view in your query because the view is updated according to changes in your DB documents only.
Using a list is also not a good option. It may seem that you can use knowledge of your request in your list to generate a different output depending on the query parameters but this is wrong because couchdb uses ETags for caching and this means that most times you will get the same answer regardless of your list parameters since the underlying documents won't have changed. There is a trick though to fool couchdb in this case and this implies using two different alternating users but I wouldn't even try this way because surely there are easier ways to achieve your objectives and you can probably solve your problem using group as a key in your map function.
I've recently added the Sphider crawler to my site in order to add search functionality. But the default search.php that comes with the distribution of Sphider that I downloaded is too plain and doesn't integrate well with the rest of my site. I have a little navigation bar at the top of the site which has a search box in it, and I'd like to be able to access Sphider's search results through that search field using Ajax. To do this, I figure I need to get Sphider to return its results in JSON format.
The way I did that is I used a "theme" that outputs JSON (Sphider supposts "theming" its output). I found that theme on this thread on Sphider's site. It seems to work, but more strict JSON parsers will not parse it. Here's some example JSON output:
{"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ", "results":[ { "idented":"false", "num":"1", "weight":"[100.00%]", "link":"http://www.avtainsys.com/articles/Triple_Contraints", "title":"Triple Contraints", "description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or", "link2":"http://www.avtainsys.com/articles/Triple_Contraints", "size":"3.3kb" }, { "num":"-1" } ], "other_pages":[ { "title":"1", "link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=", "active":"true" }, ] }
The issue is that there is a trailing comma near the end. According to this, "trailing commas are not allowed" when using PHP's json_decode() function. This JSON also failed to parse using this online formatter. But when I took the comma out, it worked and I got this better-formatted JSON:
{
"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ",
"results":[
{
"idented":"false",
"num":"1",
"weight":"[100.00%]",
"link":"http://www.avtainsys.com/articles/Triple_Contraints",
"title":"Triple Contraints",
"description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or",
"link2":"http://www.avtainsys.com/articles/Triple_Contraints",
"size":"3.3kb"
},
{
"num":"-1"
}
],
"other_pages":[
{
"title":"1",
"link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=",
"active":"true"
}
]
}
Now, how would I do this programmatically? And (perhaps more importantly), is there a more elegant way of accomplishing this? And you should know that PHP is the only language I can run on my shared hosting account, so a Java solution for example would not work for me.
In search_result.html, you can surround the , at the end of the foreach loop with condition to only print if the index is strictly less than the number of pages - 1.