How can I send an array object between pages in Framework7? I can send variable values but cannot understand how to send objects?
I have an array object like this
"phoneNumbers": [{
"number": "(555) 564-8583",
"normalizedNumber": "(555) 564-8583",
"type": "MOBILE"
}, {
"number": "(415) 555-3695",
"normalizedNumber": "(415) 555-3695",
"type": "OTHER"
}]
In route.js
{
path: '/contact/:id/:phoneNumbers/',
componentUrl: './pages/contact.html',
},
index.html
contact.html
<p>{{this.$route.params.id}}</p>
<p>{{js 'return JSON.stringify(this.$route.params.phoneNumbers)'}}</p>
In contact.html it doesnt show the array object, instead it returns this
"1"
"[object Object],[object Object]"
I want to know if it is possible to send objects using Framework7's router?
Alternate approach I took was using localStorage, but is there a way in Framework7 to send objects between pages and not only variables?
It depends on the specifics of your use case but usually I make the data I want to pass global to the app by placing it in the javascript window object. It will be available to your code on the subsequent page(s) the user enters. It's more efficient than using localstorage.
window.phoneNumbers = [{
"number": "(555) 564-8583",
"normalizedNumber": "(555) 564-8583",
"type": "MOBILE"
}, {
"number": "(415) 555-3695",
"normalizedNumber": "(415) 555-3695",
"type": "OTHER"
}]
Related
I have a JSON file that contains lots of nodes. Each node has a unique id. The following is an example skeleton structure of the JSON file.
{
"node1": {
"id": "1",
"name": "student",
"properties": {
"name": "Ram",
"lastname": "Kumar",
"age": "20"
}
},
"node2": {
"id": "2",
"name": "Teacher",
"properties": {
"name": "Ram",
"subject": "Computers"
}
}
Now, this was just sample data. Suppose, I have to find node 2, provided I have the position of the node in the file. That is it's beginning and the end. I create a read stream from the start position and the end position of the file to get the object within the complete JSON object in the file. Now, using JSON.parse function I am able to create an object within the program after parsing it.
But, now I use a writestream to make the changes in the object at the required positions after computing them.
For example, I have to edit the subject property of the teacher, and the main rule is I cannot completely write the object again. Rather, create a writestream at the position where there is subject property and just edit that value.
So, the question is how to do so? And that to with multiple positions to edit, that is editing multiple properties, without rewriting them all.
I know I might face an issue if I use multi-threading for this to simultaneously edit the multiple properties at once, but I need a work around that too.
I have a JSON data file stored locally on my PC. The file is an array of various "name": "value". For instance -
[
{
"name": "Joe",
"id": "0",
"age": "24",
},
{
"name": "Mary",
"id": "1",
"age": "25",
}
]
Now I need to make a typeahead search box for this kind of JSON data using javascript.
The code I wrote is displayed in the image:
Where "dummy" is the name of my locally stored JSON file. The code is not displaying the suggestive data from the JSON file and just gives me an input search box to enter stuff. How do I make it work? Kindly correct me and help.
I'm doing some maintenance on a project I haven't worked with before that uses .hbs files for templating and a static json file for initial data. The javascript appears to be vanilla (no backbone.js, Angular, etc.)
Is there a "correct" way of overwriting the data in that file after I get a JSON object from a REST call AFTER the page loads?
This is what is in the .hbs file:
{{> dropdown-module platform-demos.registerDemoType }}
This is what is in the static JSON file:
"registerDemoType": {
"label": "Demo Type",
"placeholder": "Choose Demo",
"inverted": "inverted",
"id": "demoId",
"items": [{
"option": "Basic Demo",
"optionValue": "basic-demo-dp"
}, {
"option": "Intermediate Demo",
"optionValue": "intermediate-demo-dp"
}, {
"option": "Advanced Demo",
"optionValue": "advanced-demo-dp"
}]
}
And then my REST call pulls back data similar to that:
"demoTypes": [{
"option": "New Demo 1",
"optionValue": "basic-demo-dp"
}, {
"option": "New Demo 2",
"optionValue": "intermediate-demo-dp"
}, {
"option": "New Demo 3",
"optionValue": "advanced-demo-dp"
}]
Is there a "correct" way of replacing the static data with the REST call data?
I feel like you have a fundamental misunderstanding of how handlebars works. Handlebars does not have any form of data binding built into it so you cannot simply change the values that were passed in and have the rendered HTML change automatically.
Instead, when your REST call is complete, you will have to update the object you are passing into your template as Hoyen suggested
registerDemoType.items = demoTypes;
And then you have to re-render this handlebars template, passing in registerDemoType as your data.
In short: You cannot access the hbs data after it has been rendered, you must re-render.
If you just want to replace it. Then all you should really need to do is something like this after the REST call responds:
registerDemoType.items = demoTypes;
I'm trying to access data within a json file using nodeJS
When I run this I get the error : TypeError: Cannot read property 'postcode' of undefined. Any Suggestions?
{
"apiName": "Restaurants",
"pages": [
{
"pageUrl": "https://url",
"results": [
{
"address": "3F Belvedere Road Coutry Hall, London, SE17GQ",
"phone": "+442076339309",
"name": "Troia",
"postcode": "SE17GQ"
}
]
}
]
}
var myData = require('./jsonFile.json');
console.log(myData.pages.result.postcode);
Try to access data as below:
console.log(myData.pages[0].results[0].postcode);
The value in the bracket is the index of element to access.
Its the common singular/plural trap, I fall for it all the time.
In your json, pages & results are arrays. You need to access these with an index. Also, you have a typo in the name.
Try this:
console.log(myData.pages[0].results[0].postcode);
This will gave you correct answer.
console.log(myData.pages[0].results[0].postcode);
I'm pulling my hair out with this. I'm not sure what is going on. I've read all the tutorials on mapping but I'm obviously missing something.
{
"address": "110",
"city": "Durham",
"id": 1,
"name": "Keep",
"persistent": true,
"salesRep": "Me",
"state": "NC",
"user": {
"email": "test#test.com",
"id": 4,
"name": "Test",
"password": "test",
"persistent": true
}
}
I've tried
ko.mapping.fromJSON(data);
and
ko.mapping.fromJS(data);
In my old code I ended up doing this.
viewModel.customers(data);
But my JSON looked different. It didn't have a nested object and it also had brackets on the beginning and end making it an array. It seems this would be pretty basic. I'm not getting any errors at all. Thanks for the help.
EDIT
http://jsfiddle.net/gjemN/
In your sample you are dealing with a JavaScript object and not JSON (string representation of it).
So, you would want to call ko.mapping.fromJS. If you are getting back an array of customers, then you could do:
ko.mapping.fromJS(data2, null, viewModel.customers)
Something like: http://jsfiddle.net/rniemeyer/BQe2z/