display data that is nested in a json object dispatched from Redux - reactjs

I am fetching a endpoint that contains nested data, i can display the business information fine. But the location part of the data, i cannot. The location data i need to iterate over and no matter what i try, i just can't seem to, i tried normalizing my data, and at first that looked like it would work, but had issues accessing the data on the front end.
below is the raw data that i grabbed from the Redux DevTools.
payload: {
data: {
id: 1,
user_id: 1,
businessName: 'LUNCH',
featured: false,
logo: 'image.jpg',
logoThumbnail: null,
deleted: false,
deletedOn: null,
stripe_subscription_id: null,
plan_id: null,
subscription_active: true,
referral_code: '8',
website: '8',
tags: null,
category: null,
business_locations: [
{
id: 1,
business_id: 1,
address: '88',
address2: null,
city: '*',
province: '*',
country: '*',
postalCode: '*',
coordinates_lat: *,
coordinates_lon: *,
phone: '*',
featured: true,
locationName: '*',
businessName: '*',
},
{
id: 2,
business_id: 1,
address: '*',
address2: null,
city: '*',
province: '*',
country: '*',
postalCode: '*',
coordinates_lat: *,
coordinates_lon: *,
phone: '*',
featured: true,
locationName: '*',
businessName: 'LUNCH',
}
],
business_images: [],
plan: null
}
}
}
this is my view simplified:
class Locations extends React.Component {
componentDidMount() {
this.props.dispatch(fetchBusinesses());
}
render() {
if (this.props.isLoading) {
return (
<SpinContainer>
<Spin size="large" />
</SpinContainer>
);
}
console.log('locations: ',this.props.business)
console.log('locations: ',this.state)
return (
<React.Fragment>
HERE IM TRYING TO MAP THE CARDS ONE FOR EACH LOCATION
<Card>
{
this.props.business.business_locations.map(location =>
<div>
<tr><td>{location.id}</td>
<td>{location.businessName}</td></tr>
</div>
)
}
</Card>
</React.Fragment>
);
}
}
function mapStateToProps(state) {
const { isLoading, data } = state.business;
return {
business: data,
isLoading,
};
}
export default connect(mapStateToProps)(Locations);
no matter what i seem to put on the front of the map function, its undefined or this.props.business.map is not a function
ive tried starting with data and business and every combo i thought
this console.log('locations: ',this.props.business)
it returns Object and the same response as above

Here's a component that renders a table on the data that you provided.
Runnable example: https://codesandbox.io/embed/k3wrx2pv55
function App() {
const data = {
"id": "asuh",
"user_id": 1,
"businessName": "Place",
"featured": false,
"logo": "image.jpg",
"logoThumbnail": null,
"deleted": false,
"deletedOn": null,
"created_at": "2017-03-23T13:00:51.000Z",
"updated_at": "2017-05-26T13:21:33.000Z",
"stripe_subscription_id": null,
"plan_id": null,
"logo_url": "***",
"logo_thumb_url": "**",
"referral_code": "**",
"website": "**",
"tags": null,
"category": null,
"business_locations": [
{
"id": 1,
"business_id": 1,
"address": "*",
"address2": null,
"city": "*",
"province": "*",
"country": "*",
"postalCode": "*",
"coordinates_lat": 45,
"coordinates_lon": 75,
"phone": "**********",
"locationName": "***",
"businessName": "ACME",
},
{
"id": 2,
"business_id": 1,
"address": "***",
"address2": null,
"city": "***",
"province": "***",
"country": "**",
"postalCode": "***",
"coordinates_lat": 45,
"coordinates_lon": 75,
"phone": "**********",
"locationName": "Secondary",
"businessName": "ACME",
}
],
"business_images": [],
"plan": null
};
return (
<div className="App">
<table>
<thead>
<tr><td>id</td></tr>
</thead>
<tbody>
{data.business_locations.map(location => <tr><td>{location.id}</td><td>{location.businessName}</td></tr>)}
</tbody>
</table>
</div>
);
}

Related

How to update a field in Fauna Database

I am working in fauna DB. I have a confusion for writing an query to update a field . From the given array object I want to update the Boolean fields ( "presentation", "keyTakeaways", "whitepaper", "downloadAll") with the reference to the SegmentAnonymousID. I am adding the code that I tried. Can someone help me to sort out the problem.
[{
"ref": Ref(Collection("Downloads"), "322568726157197900"),
"ts": 1643884359510000,
"data": {
"SegmentAnonymousID": "57f22fc0-2ace-4522-b0a6-4d0cd45cfc3a",
"response": {
"firstname": "test ",
"lastname": "test 1",
"email": "test#gmail.com",
"company": "test",
"presentation": false,
"keyTakeaways": false,
"whitepaper": false,
"downloadAll": false
}
}
},
{
"ref": Ref(Collection("Downloads"), "322484599970071113"),
"ts": 1643870069845000,
"data": {
"SegmentAnonymousID": "15ba9e0d-e646-4d31-beaa-b2a4d3eac56f",
"response": {
"firstname": "test 4",
"lastname": "test4",
"email": "test#gmail.com",
"company": "test",
"presentation": false,
"keyTakeaways": false,
"whitepaper": false,
"downloadAll": false
}
}
}]
The code I tried
const result = await faunaClient.query(
q.Update(
q.Select("ref", q.Get(q.Match(q.Index("SegmentAnonymousID"), "57f22fc0-2ace-4522-b0a6-4d0cd45cfc3a")), { data: { response: { "presentation": true } } })
)
)
Your query has brackets in the wrong place. As written, the object containing the new data appears as the "default" value for the Select call. You'll likely have better success with this update (reformatted to show function calls and parameters better):
const result = await faunaClient.query(
q.Update(
q.Select(
"ref",
q.Get(
q.Match(
q.Index("SegmentAnonymousID"),
"57f22fc0-2ace-4522-b0a6-4d0cd45cfc3a"
)
)
),
{
data: {
response: {
presentation: true
}
}
}
)
)

AngularJS Recursive Data Structure Traversal with Unique Keys and Arrays

I believe this question is unique in that I am attempting to traverse a data structure containing objects with unique keys and arrays. I've seen other questions regarding this subject, but they reference simple data structures with multiple levels the same object.
Ultimately, I would like to be able to generate a form from any given data structure simply by traversing it as an object or an array.
I've created two directives, one for traversing an object and the other for traversing an array. Basically I'm trying to pass in the array or object as a parameter into the directive's isolated scope, then generate the associated form fields by referencing the scope variable scope.context. I'm not getting any errors, but I'm also not getting the expected result.
Am I approaching this the right way?
Any thoughts or suggestions would be greatly appreciated.
HTML
<div ng-app="app" ng-controller="CreateFormController">
<div traverse-object="{{model.form}}"></div>
<script type="text/ng-template" id="traverseObject.tmpl.html">
<div ng-repeat="(key, value) in context">
<div ng-switch="getTypeOf(value)">
<div ng-switch-when="string">
{{key}} string
</div>
<div ng-switch-when="null">
{{key}} null
</div>
<div ng-switch-when="object">
{{key}} object
<div traverseObject = "{{value}}"></div>
</div>
<div ng-switch-when="array">
{{key}} array
<div traverseArray = "{{value}}"></div>
</div>
<div ng-switch-default="true">
{{key}} other
</div>
</div>
</div>
</script>
<script type="text/ng-template" id="traverseArray.tmpl.html">
<div ng-repeat="item in context">
<div ng-switch="getTypeOf(item)">
<div ng-switch-when="string">
{{item}}
</div>
<div ng-switch-when="null">
{{item}}
</div>
<div ng-switch-when="object">
<div traverseObject = "{{item}}"></div
</div>
<div ng-switch-when="array">
<div traverseArray = "{{item}}"></div>
</div>
<div ng-switch-default="true">
{{item}}
</div>
</div>
</div>
</script>
</div>
JS
var json = {
"ProjectName": null,
"ProjectNumber": null,
"BillingAU": null,
"BillingMailCode": null,
"BorrowersName": null,
"Comment": null,
"CompanyNumber": null,
"DesiredDeliveryDate": null,
"LendingGroup": "",
"LendingOffice": null,
"LoanAmount": null,
"LoanAmountOther": null,
"LoanCloseDate": null,
"LoanNumber": null,
"LoanTerm": null,
"Obligor": null,
"OriginatingAU": null,
"PreviousProjectNumber": null,
"PreviousReport": null,
"TransactionComments": null,
"IsInvolvement": null,
"InvolvementType": "",
"IsFFESecured": null,
"IsJuniorLien": null,
"IsParticipationLoan": null,
"IsWithCrossCollateral": null,
"IsWithLoanGuarantor": null,
"TotalParticipationLoanAmount": null,
"LienPosition": null,
"LoanExist": "",
"LoanType": "",
"OtherLien": null,
"PriorityApproval": "",
"ProjectPurpose": "",
"LoanTransType": "",
"ServiceRequestPeople": [
{
"PersonType": "Account Officer",
"FirstName": null,
"LastName": null,
"Email": null,
"Company": null,
"CompanyNumber": null,
"ImportUserId": null,
"PhoneNumber": "",
"OtherNumber": "",
"FaxNumber": "",
"Address": {
"StreetAddress": "",
"StreetAddress2": "",
"City": "",
"State": "",
"ZipCode": "",
"County": "",
"Nation": "United States",
"Links": []
},
"Links": []
}
],
"Properties": [
{
"DevelopmentName": null,
"PropertyTypesMajor": "",
"PropertyTypes": "",
"PropertyAddress": {
"StreetAddress": "",
"StreetAddress2": "",
"City": "",
"State": "",
"ZipCode": "",
"County": "",
"Nation": "United States",
"Links": []
},
"PropertyDescription": null,
"PropertyCondition": "",
"PropertyRemark": null,
"PropertyStatus": "",
"PropertyTenancy": "",
"ExternalReferenceNumber": null,
"FhaCaseNumber": null,
"LegalComment": null,
"NumberOfBuildings": null,
"NumberOfTenants": null,
"NumberOfUnits": null,
"Occupancy": null,
"PriorSaleDate": "",
"PriorSalePurchaseAmount": null,
"TotalPurchaseAmount": null,
"ProposedUse": null,
"CurrentUse": null,
"Stories": null,
"YearBuilt": null,
"YearLastRenovate": null,
"RentableArea": null,
"Zoning": null,
"ZoningCodeDescription": null,
"MapNumber": null,
"IsForSale": null,
"ListSaleAmount": null,
"IsLegalDescription": null,
"IsPendingSale": null,
"PendingSaleAmount": null,
"PendingSaleDate": "",
"IsRenovationDemo": null,
"RenovationDemoDescription": null,
"IsFloodPlain": null,
"FloodPlainDescription": null,
"FloodPlainSurveyed": null,
"IsGroundLease": null,
"ParkingType": [
""
],
"PrimaryImprovement": {
"Measurement": 0,
"Unit": "",
"Links": []
},
"SecondaryImprovement": {
"Measurement": 0,
"Unit": "",
"Links": []
},
"LandSize": {
"Measurement": 0,
"Unit": "",
"Links": []
},
"ExcessLandSize": {
"Measurement": 0,
"Unit": "",
"Links": []
},
"PropertyParcel": [
{
"ParcelNumber": null,
"Size": 0,
"Links": []
}
],
"PropertyPeople": [
{
"PersonType": "",
"FirstName": null,
"LastName": null,
"Email": null,
"PhoneNumber": "",
"OtherNumber": "",
"FaxNumber": "",
"Affiliation": null,
"Links": []
}
],
"Services": [
{
"ServiceType": null,
"Comments": null,
"SentDate": "",
"DesiredDeliveryDate": "",
"Links": []
}
],
"Links": []
}
],
"Links": []
}
var app = angular.module('app', [])
app.controller('CreateFormController', function($scope){
$scope.model = {}
$scope.model.form = json
})
app.directive("traverseObject", function(){
return {
scope: {
traverseObject: '#'
},
templateUrl: 'traverseObject.tmpl.html',
link: function(scope){
scope.getTypeOf = function(value){
if(value === null) {
return "null";
}
if(Array.isArray(value)) {
return "array";
}
return typeof value;
}
scope.context = JSON.parse(scope.traverseObject)
}
}
})
app.directive("traverseArray", function(){
return {
scope: {
traverseArray: '#'
},
templateUrl: 'traverseArray.tmpl.html',
link: function(scope){
scope.getTypeOf = function(value){
if(value === null) {
return "null";
}
if(Array.isArray(value)) {
return "array";
}
return typeof value;
}
scope.context = JSON.parse(scope.traverseArray)
}
}
})

How to display nested objects and or arrays in angular 2

I am getting json data from the back-end(api). And i want to display this with ngFor. I got an error message like: "Cannot find a differ supporting object '[object Object]'" from the console.
later I tried this:
#Pipe({
name: 'mapToIterable'
})
export class MapToIterable implements PipeTransform{
transform(map: {}, args: any[] = null): any {
if (!map)
return null;
return Object.keys(map)
.map((key) => ({ 'key': key, 'value': map[key] }));
}
}
and then in my view:
<li *ngFor="let detail of getEventDetails | mapToIterable">
Creator Email: {{detail.creator.email}}
</li>
this time i didn't get an error but there is no display values of {{detail.creator.email}}
Data from back-end
{
"banner_image": null,
"categories": [],
"creator": {
"email": "email#email.com",
"first_name": "Prince",
"gender": true,
"id": 15,
"last_name": "Odame",
"subscribed_categories": [],
"watchlist": []
},
"creator_id": 15,
"description": "Learn how to install solar panels in 3 days and make real money all your lifetime",
"faqs": [],
"id": 6,
"is_verified": true,
"max_tickets_per_user": 1,
"shows": [
{
"address": "Engineering Auditorium, College of Engineering, KNUST, Kumasi",
"city": "Kumasi",
"country": "Ghana",
"end_time": "2016-08-03T14:30:00+00:00",
"event_id": 6,
"id": 5,
"is_online": false,
"start_time": "2016-08-01T08:30:00+00:00",
"state": "Ashanti",
"ticket_types": [],
"venue": "Engineering Auditorium"
}
],
"tags": [],
"title": "Solar Panels Installation Workshop"
}
Thanks in advance
You probably just want to do this:
<li>Creator Email: {{getEventDetails.creator.email}}</li>
And for the arrays:
<li *ngFor="let show of getEventDetails?.shows">
Show ID: {{show.id}}
</li>

Yii2 return JSON format with array inside an array

I am trying to get these data below,
Table relations:
people(one) <---> (many) people_info (one) <---> (many) people_contact
in the following format,
people: {
p_id: 10,
p_price: 3.99,
people_info : [
{
pl_id: 3,
pl_state: 2,
pl_district: 6,
pl_latitude: 6.323434,
pl_longitude: 108.23499,
people_contact: [
{
plc_id: 2
},
{
plc_id: 1
}
]
},
{
pl_id: 2,
pl_state: 7,
pl_district: 12,
pl_latitude: 6.000434,
pl_longitude: 108.9910003,
people_contact: [
{
plc_id: 5
},
{
plc_id: 9
}
]
}
]
}
Currently with these controller codes,
class PeopleController extends Controller
{
public function actionPeople($params){
Yii::$app->response->format = Response::FORMAT_JSON;
....//some other codes//.....
$people= People::find()->select(['p_id', 'p_price'] )->where(['p_id' => $itemId])->one();
$info= PeopleContact::find()->with(['plPeople'])->asArray([])->all();
return array(
'people' => $people,
'info' => $info,
);
}
}
I got these,
"people": {
"p_id": "3",
"p_price": "32.42"
}, "locations": [{
"pl_id": "1",
"pl_people": "3",
"pl_title": "",
"pl_latitude": "6.16438700000000000000",
"pl_longitude": "102.28314649999993000000",
"pl_place": null,
"pl_premise": null,
"pl_street": "1",
"pl_area": "1",
"pl_postcode": "1",
"pl_district": "1",
"pl_state": "3",
"pl_country": 1,
"place": null,
"premise": null,
"street": null,
"area": null,
"postcode": null,
"district": null,
"state": null,
"country": "United Kingdom",
"contacts": [{
"plc_name": "joe",
"plc_phone": "123456",
"plc_email": null
}]
}]
}
How do I achieve it in the format mentioned at the top?
$output;
$people=People::find()->select(['p_id', 'p_price'] )->asArray()->all();
foreach($people as $person) {
$infos = PersonInfo::find()->where(['person_id' => $person->id])->asArray()->all();
foreach($infos as $info) {
$contacts = PersonContact::find()->where(['person_info_id' => $info->id])->asArray()->all();
foreach($contacts as $contact) {
$info['contacts'][] = $contact;
}
$person['info'][] = $info
}
$output['people'][] = $person
}
return $output;
You should loop through and fetch data like this: people > info > contact each next level relying on info fetched from the previous one. Then store it in the format you want such as demonstrated above.
This will output something like:
"people": [{
...
"info": [{
...
"contacts": [{
...
},{
...
}]
}]
},{
...
}]

Can I use this callback result with Backbone?

I hope someone can help me as Ive been trying for well over a week to see if I can use this callback result to populate a Backbone model/Collection.
Specifically, I want to list the Area Name and the AreaGUID - I also want to get the total price from the PriceMatrix.
PriceMatrix aside - I'd even settle for the Area Name and AreaGUID as I would hopefully learn how to traverse this JSON, and make collections from its nested values.
JSON:
$esro.GenericCallBack: (
{
"CustomCallback": "getEventDescriptionCallback",
"ErrorDescription": "",
"EvalMe": "",
"HasError": false,
"Result": {
"__type": "EventItemEntity:#eSRO.Code.JSonEntities",
"Areas": [
{
"Area": {
"AreaGuid": "9fc8ae7e-2059-4eb6-8c6f-527f3a0ea5fc",
"AreaMapGuid": "97a511ac-6b36-4223-9a08-5e099ebcd6ee",
"DefaultSeatingType": 10,
"GADefaultSettings": {
"Capacity": 10000,
"PriceLevelGuid": null,
"Restrictions": null
},
"Name": {
"Value": "Balc Fest 2013 Murmur"
},
"NameForReports": {
"Value": "Balc Fest 2013 Murmur"
},
"Ordinal": 0,
"ParentHallGuid": "b4df2722-fe70-4b03-bf9b-4a35286b1330",
"StandId": null,
"StandName": null
},
"AreaMap": {
"AreaMapGuid": "97a511ac-6b36-4223-9a08-5e099ebcd6ee",
"DescriptionLevel": 1,
"GASettings": null,
"GateCombinations": null,
"IncludedInMultipleVersions": false,
"Name": {
"Value": "Balc Fest 2013 Murmur"
},
"NumberOfSeats": 180,
"ParentAreaGuid": "9fc8ae7e-2059-4eb6-8c6f-527f3a0ea5fc",
"SeatingType": 20,
"Sectors": null,
"Status": 20,
"TurnstileCombinations": null,
"PriceLevelIds": [
"7ec859f6-2294-4b0f-b57a-9fa120c221a6",
"5112ea83-0880-4785-b93c-21e18c7a667b"
]
},
"Connector": {
"AreaLevelViewXaml": null,
"AreaMapGuid": "97a511ac-6b36-4223-9a08-5e099ebcd6ee",
"BackgroundImageIds": null,
"BestAvailableReservation": true,
"HallLevelViewXaml": "<Properties><Width>770</Width><Height>185</Height><X>0</X><Y>0</Y><Angle>0</Angle><Xaml /><AreaLabel /><StageSide /><IfShowLabel /></Properties>",
"Ordinal": 1,
"ParentVersionGuid": "b0352f6b-056d-4577-b3c1-c0ae8773bbb5",
"StaticViewXamls": null,
"ViewFromAreaImageId": null
},
"DependencyTags": [
"AreaMap=97a511ac-6b36-4223-9a08-5e099ebcd6ee",
"SeatingPlan=0aaa76eb-0b49-4ab7-9ef7-767b8400fb11",
"Area=9fc8ae7e-2059-4eb6-8c6f-527f3a0ea5fc;SeatingPlan=0aaa76eb-0b49-4ab7-9ef7-767b8400fb11",
"Show=37f6bcd8-c63c-4967-9982-3c3ae4c204e4",
"Area=9fc8ae7e-2059-4eb6-8c6f-527f3a0ea5fc;Show=37f6bcd8-c63c-4967-9982-3c3ae4c204e4",
"HallVersion=b0352f6b-056d-4577-b3c1-c0ae8773bbb5",
"Area=9fc8ae7e-2059-4eb6-8c6f-527f3a0ea5fc;HallVersion=b0352f6b-056d-4577-b3c1-c0ae8773bbb5"
],
"ImageUris": []
},
{
"Area": {
"AreaGuid": "063b1dad-e789-4857-852f-5e733fa84865",
"AreaMapGuid": "6c716990-9686-4304-a015-4db450f2fbd5",
"DefaultSeatingType": 10,
"GADefaultSettings": {
"Capacity": 1078,
"PriceLevelGuid": null,
"Restrictions": null
},
"Name": {
"Value": "ORCH Fest 2013 Murmur"
},
"NameForReports": {
"Value": "ORCH Fest 2013 Murmur"
},
"Ordinal": 0,
"ParentHallGuid": "b4df2722-fe70-4b03-bf9b-4a35286b1330",
"StandId": null,
"StandName": null
},
"AreaMap": {
"AreaMapGuid": "6c716990-9686-4304-a015-4db450f2fbd5",
"DescriptionLevel": 1,
"GASettings": null,
"GateCombinations": null,
"IncludedInMultipleVersions": false,
"Name": {
"Value": "ORCH Fest 2013 Murmur"
},
"NumberOfSeats": 425,
"ParentAreaGuid": "063b1dad-e789-4857-852f-5e733fa84865",
"SeatingType": 20,
"Sectors": null,
"Status": 20,
"TurnstileCombinations": null,
"PriceLevelIds": [
"7ec859f6-2294-4b0f-b57a-9fa120c221a6",
"5c9fa97b-c47c-463a-8da4-78956a241e5e",
"5112ea83-0880-4785-b93c-21e18c7a667b",
"01b28b26-b09c-46c3-b75a-02331c1231f9",
"e717cf91-986d-486d-b533-1309123fbf78"
]
},
"Connector": {
"AreaLevelViewXaml": null,
"AreaMapGuid": "6c716990-9686-4304-a015-4db450f2fbd5",
"BackgroundImageIds": null,
"BestAvailableReservation": true,
"HallLevelViewXaml": "<Properties><Width>770</Width><Height>185</Height><X>0</X><Y>195</Y><Angle>0</Angle><Xaml /><AreaLabel /><StageSide /><IfShowLabel /></Properties>",
"Ordinal": 2,
"ParentVersionGuid": "b0352f6b-056d-4577-b3c1-c0ae8773bbb5",
"StaticViewXamls": null,
"ViewFromAreaImageId": null
},
"DependencyTags": [
"AreaMap=6c716990-9686-4304-a015-4db450f2fbd5",
"SeatingPlan=0aaa76eb-0b49-4ab7-9ef7-767b8400fb11",
"Area=063b1dad-e789-4857-852f-5e733fa84865;SeatingPlan=0aaa76eb-0b49-4ab7-9ef7-767b8400fb11",
"Show=37f6bcd8-c63c-4967-9982-3c3ae4c204e4",
"Area=063b1dad-e789-4857-852f-5e733fa84865;Show=37f6bcd8-c63c-4967-9982-3c3ae4c204e4",
"HallVersion=b0352f6b-056d-4577-b3c1-c0ae8773bbb5",
"Area=063b1dad-e789-4857-852f-5e733fa84865;HallVersion=b0352f6b-056d-4577-b3c1-c0ae8773bbb5"
],
"ImageUris": []
}
],
"Id": "0aaa76eb-0b49-4ab7-9ef7-767b8400fb11",
"Pricing": {
"PriceLevelAxis": [
{
"key": "7ec859f6-2294-4b0f-b57a-9fa120c221a6",
"value": "C Price"
},
{
"key": "5112ea83-0880-4785-b93c-21e18c7a667b",
"value": "B Price"
},
{
"key": "5c9fa97b-c47c-463a-8da4-78956a241e5e",
"value": "W/C Wheel chair** C Price"
},
{
"key": "01b28b26-b09c-46c3-b75a-02331c1231f9",
"value": "W/C Wheel chair** B Price"
},
{
"key": "e717cf91-986d-486d-b533-1309123fbf78",
"value": "A Price"
}
],
"PriceMatrix": [
[
{
"ListPrice": "<Money amount=\"25\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>",
"PriceModifiers": null,
"TotalPrice": "<Money amount=\"25\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>"
},
{
"ListPrice": "<Money amount=\"40\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>",
"PriceModifiers": null,
"TotalPrice": "<Money amount=\"40\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>"
},
{
"ListPrice": "<Money amount=\"25\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>",
"PriceModifiers": null,
"TotalPrice": "<Money amount=\"25\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>"
},
{
"ListPrice": "<Money amount=\"40\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>",
"PriceModifiers": null,
"TotalPrice": "<Money amount=\"40\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>"
},
{
"ListPrice": "<Money amount=\"55\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>",
"PriceModifiers": null,
"TotalPrice": "<Money amount=\"55\" currencyCode=\"\" xmlns=\"http://foundation.toptix.com/2007\"/>"
}
]
],
"PriceModifierDescriptions": [],
"PriceTypeAxis": [
{
"key": "5ea4520c-d647-4a8c-b392-3f52910396d4",
"value": "FULL"
}
]
},
"SeatingTypeEnum": [
{
"key": "GA",
"value": 10
},
{
"key": "BA",
"value": 20
}
],
"ShowName": "Murmurs"
}
}
)
Model and Collection currently:
Entities.Area = Backbone.Model.extend({
defaults: {
Id: '',
AreaGuid:'',
AreaMapGuid: '',
AreaNameValue: '',
DefaultSeatingType: '',
PriceLevelIds: '',
MinPriceLevelIds: '',
MaxPriceLevelIds: '',
SeatingPlan: '',
BestAvailableReservation: '',
PriceLevelAxis: '',
PriceMatrix: '',
ShowName: '',
FacilityFee: '',
ServiceFee : '',
SeatPriceTotal : ''
Collection:
Entities.AreaCollection = Backbone.Collection.extend({
initialize: function() {
this.on('all', function(e) { console.log("Area event: " +e); });
},
url: "./murmurs.json",
model: Entities.Area,
parse : function(response){
return response.Result;
}
});
Please help if you can - and thanks!
you can have a straightforward hierarchical design like this ...
var RootModel = Backbone.Model.extend({
default:
{
"CustomCallback": "",
"ErrorDescription": "",
"EvalMe": "",
"HasError": false,
"Result": null
}
});
var ResultModel = Backbone.Model.extend({
default:
{
"__type": "",
"Areas": []
}
});
var AreaCollection = Backbone.Collection.extend({
model: areaDetailsModel
});
var AreaDetailsModel = Backbone.Model.extend({
default:
{
"Area": null,
"AreaMap": null,
"Connector": null
}
});
var AreaModel = Backbone.Model.extend({
default:
{
"AreaGuid": "",
"AreaMapGuid": "",
"DefaultSeatingType": 0,
"GADefaultSettings": null,
"Name": "",
"NameForReports": "",
"Ordinal": 0,
"ParentHallGuid": "",
"StandId": null,
"StandName": null
}
});
var AreaMapModel = Backbone.Model.extend({
default:
{
"AreaMapGuid": "",
"DescriptionLevel": "",
"GASettings": null,
"GateCombinations": null,
"IncludedInMultipleVersions": false,
"Name": "",
"NumberOfSeats": 0,
"ParentAreaGuid": "",
"SeatingType": 0,
"Sectors": null,
"Status": 0,
"TurnstileCombinations": null,
"PriceLevelIds": []
}
});
....
Then, you can assign your callback result to the rootModel.
var rootModel= new RootModel("your callback to JSON");
var resultModel = new ResultModel(rootModel.get("Result"));
var areas = new AreaCollection(resultModel.get("Areas"));
var areaDetail = areas.at(0);
var area = new AreaModel(areadetail.get("area");
...
I have not tried it but this may work.
var area_detail_model = Backbone.Model.extend({
defaults: {
AreaGuid: null,
AreaMapGuid: null
}
});
var area_map_model = Backbone.Model.extend({
defaults: {
AreaMapGuid: null,
DescriptionLevel: null
}
});
var area_model = Backbone.Model.extend({
defaults: {
Area: new area_detail_model(),
AreaMap: new area_map_model(),
Connector: [] // can declare a model for connector also
DependencyTags: [] // when using an array we can write bb_obj.get('Attr').ArrayItemName
},
parse : function(response){
// at this point only area models exist, i.e from area collection each area model
// is passed to this parse method
response.Area = new area_model(response.Area);
response.AreaModel = new area_map_model(response.AreaModel);
return response;
}
});
var area_collection = Backbone.Collection.extend({
model: area_model,
url: '/Areas',
parse : function(response){
return response; // place a break point and check out the contents of response
}
});
var result_model = Backbone.Model.extend({
defaults: {
Areas: new area_collection(),
Id: null,
Pricing: null,
},
url: '/GetResults',
parse : function(response){
response.Result.Areas = new area_collection(response.Result.Areas);
return response.Result; // this will return only the Result section from your JSON
}
});
var result_obj = new result_model();
result_obj.fetch();
So when the response is fetched then the result_model parse function will only pick Result from JSON and then we create a collection out the areas and the corresponding parse for each collection and method will be called in order. Let me know if it works.

Resources