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)
}
}
})
Related
From this file :
[
{
"network": "X.X.X.1",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"status": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"status": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"status": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"status": "available"
},
{
"foo1": "10.0.0.5",
"status": "available"
},
{
"foo1": "10.0.0.6",
"status": "available"
},
{
"foo1": "10.0.0.7",
"status": "available"
}
],
"full": false,
"id": "xxx"
},
{
"network": "X.X.X.2",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"status": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"status": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"status": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"status": "available"
},
{
"foo1": "10.0.0.5",
"status": "available"
},
{
"foo1": "10.0.0.6",
"status": "available"
},
{
"foo1": "10.0.0.7",
"status": "available"
}
],
"full": false,
"id": "xxx"
}
]
# Just an example, there is more lines in my file
I can keep the informations that I want :
cat myfile | jq 'map({network, full})'
[
{
"network": "X.X.X.1",
"full": false
},
{
"network": "X.X.X.2",
"full": false
}
]
Now I'm looking for a tip to count and display some values. For example, I would like to display the number of reserved, allocated and available like that :
[
{
"network": "X.X.X.1",
"full": false
reserved: 3
available: 4
},
{
"network": "X.X.X.2",
"full": false
reserved: 3
available: 4
}
]
I've look everywhere and I found no good example to do that...
Some one to show me how can I have this output ?
Thanks !
Use reduce to count statuses.
map({network, full} +
reduce .ipAddressTab[].status as $s ({}; .[$s] += 1))
Online demo
You can change {} to {reserved: 0, available: 0} to maintain a consistent order of keys among all the entries.
One way to do this would be to use a function to count the objects of each type
def f($path; $val): $path | map(select(.status == $val)) | length;
map({network, full, reserved: f(.ipAddressTab; "reserved"), available: f(.ipAddressTab; "available")})
jqplay - Demo
The function f takes a path and the status string to be looked-up then gets the length of the objects in the array.
With oguz ismail's suggestion to avoid repetition
def f($val): map(select(.status == $val)) | length;
map({network, full} + (.ipAddressTab | { reserved: f("reserved"), available: f("available")}))
When counting a potentially large number of objects, it's usually better to
use stream-oriented filters so as to avoid constructing arrays. These two are often useful as a pair, though in the present case defining just count/1 by itself would be sufficient:
def sigma(s): reduce s as $x (0; .+$x);
def count(s): sigma(s|1);
To achieve the stated goal, one can now simply write the specification as a program:
map({network,
full,
reserved: count(.ipAddressTab[] | select(.status == "reserved")),
available: count(.ipAddressTab[] | select(.status == "available"))
})
Generalization
Now for a little jq magic -- no references to specific "status" values at all:
def countStatus($s):
{($s): count(.ipAddressTab[] | select(.status == $s))};
def statuses: [.ipAddressTab[].status] | unique;
map( {network, full}
+ ([countStatus(statuses[])] | add) )
total_available
In a comment, a question about showing total_available was asked.
To add a total_available key to each object, you could append the
following to either of the above pipelines:
| {total_available: sigma(.[] | .available)} as $total
| map(. + $total)
I am quite new with Postman and not a particularly good programmer. I am testing an API and trying to validate a part of a response that has an array in it looking like this:
"processors": [
{
"name": "ARTPEC-5",
"type": "SOC",
"url": null,
"releaseNotes": null,
"cdnUrl": null,
"cdnReleaseNotes": null
},
{
"name": "SSL",
"type": "SOC",
"url": null,
"releaseNotes": null,
"cdnUrl": null,
"cdnReleaseNotes": null
},
{
"name": "ARTPEC-7",
"type": "SOC",
"url": null,
"releaseNotes": null,
"cdnUrl": null,
"cdnReleaseNotes": null
}
]
Now, I would like to validate that the array comes with the above objects. They may come in any order in the array, so I cannot refer to the objects using index like jsonData.processors[0] and then validate them one by one like that. I need a general validation method. I have tried this that did not work:
pm.test("Check if the response has processors", function () {
pm.expect(jsonData.processors).to.have.members([
{
"name": "ARTPEC-5",
"type": "SOC",
"url": null,
"releaseNotes": null,
"cdnUrl": null,
"cdnReleaseNotes": null
},
{
"name": "SSL",
"type": "SOC",
"url": null,
"releaseNotes": null,
"cdnUrl": null,
"cdnReleaseNotes": null
},
{
"name": "ARTPEC-7",
"type": "SOC",
"url": null,
"releaseNotes": null,
"cdnUrl": null,
"cdnReleaseNotes": null
}]);
});
This approach only gives me the cryptic error message AssertionError: expected [ Array(3) ] to have the same members as [ Array(3) ]
By using _.differenceWith() you will get empty array if there is no difference in the array objects and their properties. That you can assert as -
var _ = require('lodash')
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 1, 'y': 2 }];
var arr = _.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
console.log(arr);
console.log(objects.length);
// assert that difference should be empty
pm.test("array difference", function () {
pm.expect([]).to.eql(arr)
});
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>
);
}
I'm creating a laravel application and I'm trying to access a child object's properties in my view using Angular. I can get the whole data in my view (including child objects), but I'm having trouble when trying to display it using Angular.
controller:
public function index(Request $request)
$lang = $request->input('lang') ?? 'pt';
$interestTranslations = Interest::with([
'interesttranslations' => function($query) use ($lang) {
$query->where('languageCode', $lang);
}
])->get()->toArray();
return ['translatedInterests' => $interestTranslations];
}
}
js file:
app..controller("Interests", function ($scope, $rootScope, HobbiesService) {
var lang = $rootScope.lang;
HobbiesService.newData(lang).then(function (response) {
$rootScope.interests = response["data"].translatedInterests;
});
})
html file (interest.interesttranslations works but interest.interesttranslations.name_or_other_property doesn't):
<p style="color: white;">{{interests}}</p> <!-- this shows the whole json datam including the child objects-->
<div class="no-padding owl-carousel" id="hobbies-container" ng-controller="Interests">
<div ng-repeat="interest in interests">
<img src="/img/about/{{ interest.imageName }}" alt="{{ interest.imageName }}" /> <!--this works-->
<p style="color: white;">{{ interest.interesttranslations.name }}</p> <!--this doesn't work-->
</div>
</div>
json displayed in my view when using {{ interests }}:
[{
"id": 1,
"applicant_id": 1,
"imageName": "friends.png",
"created_at": null,
"updated_at": null,
"interesttranslations": [{
"interest_id": 1,
"languageCode": "pt",
"name": "explorar com amigos",
"created_at": null,
"updated_at": null
}]
}, {
"id": 2,
"applicant_id": 1,
"imageName": "world.png",
"created_at": null,
"updated_at": null,
"interesttranslations": [{
"interest_id": 2,
"languageCode": "pt",
"name": "viajar",
"created_at": null,
"updated_at": null
}]
},
{
"id": 3,
"applicant_id": 1,
"imageName": "tv-shows.png",
"created_at": null,
"updated_at": null,
"interesttranslations": [{
"interest_id": 3,
"languageCode": "pt",
"name": "séries televisivas",
"created_at": null,
"updated_at": null
}]
}
]
Thanks in advance!
The property interesttranslations is an array, not an object.
In the view try
{{ interest.interesttranslations[0].name }}
Or use an inner ng-repeat to iterate that array
<p ng-repeat="item in interest.interesttranslations">
{{item.name}}
</p>
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.