Knockout.js and mapping plugin not deep translating
I use a json object to handle my menus and breadcrumbs. Now below you can see that the first two "Navigation" nodes are observable, but the last one isn't. It is just a regular array for some reason. Does the mapping plugin not deep clone the object?
Firebug output:
app.viewModel.members.layout().Navigation()[2].Navigation()[1].Navigation() <-- errors
Initialization:
app.viewModel.members.layout(ko.mapping.fromJS(json.Layout));
json.Layout JSON:
{
"Layout": {
"Navigation": [
{
"ID": "Dashboard",
"Type": "Menu",
"Route": "dashboard",
"Title": "Dashboard"
},
{
"ID": "Events",
"Type": "Menu",
"Route": "events",
"Title": "Events",
"Navigation": [
{
"ID": "AddEvent",
"Type": "Action",
"Route": "events/event",
"Title": "Add Event",
"Label": "+ Add Event",
"Order": "1"
},
{
"ID": "EditEvent",
"Type": "Item",
"Route": "events/event",
"Parameters": "eventid",
"Title": "Edit Event",
"Navigation": [
{
"ID": "EventGymCourts",
"Type": "Menu",
"Route": "events/event/gymcourts",
"Title": "Locations",
"Parameters": "eventid",
"Navigation": [
{
"ID": "AddEventGymCourt",
"Type": "Action",
"Route": "events/event/gymcourts/gymcourt",
"Title": "Add Location",
"Parameters": "eventid",
"Label": "+ Add Location",
"Order": "1"
},
{
"ID": "EditEventGymCourt",
"Type": "Item",
"Route": "events/event/gymcourts/gymcourt",
"Parameters": "eventid,gymcourtid",
"Title": "Edit Location"
}
]
},
{
"ID": "Teams",
"Type": "Menu",
"Route": "events/event/teams",
"Title": "Teams",
"Parameters": "eventid",
"Navigation": [
{
"ID": "AddTeam",
"Type": "Action",
"Route": "events/event/teams/team",
"Title": "Add Team",
"Parameters": "eventid",
"Label": "+ Add Team",
"Order": "1"
},
{
"ID": "EditTeam",
"Type": "Item",
"Route": "events/event/teams/team",
"Parameters": "eventid,teamid",
"Title": "Edit Team"
}
]
},
{
"ID": "Pools",
"Type": "Menu",
"Route": "events/event/pools",
"Title": "Pools",
"Parameters": "eventid",
"Navigation": [
{
"ID": "AddPool",
"Type": "Action",
"Route": "events/event/pools/pool",
"Title": "Add Pool",
"Parameters": "eventid",
"Label": "+ Add Pool",
"Order": "1"
},
{
"ID": "EditPool",
"Type": "Item",
"Route": "events/event/pools/pool",
"Parameters": "eventid,poolid",
"Title": "Edit Pool"
}
]
},
{
"ID": "Brackets",
"Type": "Menu",
"Route": "events/event/brackets",
"Title": "Brackets",
"Parameters": "eventid",
"Navigation": [
{
开发者_运维知识库 "ID": "AddBracket",
"Type": "Action",
"Route": "events/event/brackets/bracket",
"Title": "Add Bracket",
"Parameters": "eventid",
"Label": "+ Add Bracket",
"Order": "1"
},
{
"ID": "EditBracket",
"Type": "Item",
"Route": "events/event/brackets/bracket",
"Parameters": "eventid,bracketid",
"Title": "Edit Bracket"
}
]
}
]
}
]
},
{
"ID": "Gyms",
"Type": "Menu",
"Route": "gyms",
"Title": "Locations",
"Navigation": [
{
"ID": "AddGym",
"Type": "Action",
"Route": "gyms/gym",
"Title": "Add Location",
"Label": "+ Add Gym",
"Order": "1"
},
{
"ID": "EditGym",
"Type": "Item",
"Route": "gyms/gym",
"Parameters": "gymid",
"Title": "Edit Location",
"Navigation": {
"ID": "EditMap",
"Type": "Menu",
"Route": "gyms/gym/map",
"Parameters": "gymid",
"Title": "Map"
}
}
]
}
]
}
}
Update:
Looking closer it looks like since that "Navigation" has only one node, it makes it one object and not an array like the others. How could I remedy this? Using create in the mapping plugin?
Well I used the create method in the mapping plugin.
var mapping = {
'Navigation': {
create: function (options) {
if (options.data.Navigation) {
if (options.data.Navigation instanceof Array) {
options.data = ko.mapping.fromJS(options.data, mapping);
}
else{
options.data.Navigation = [options.data.Navigation];
}
}
return ko.mapping.fromJS(options.data);
}
}
};
app.viewModel.members.layout(ko.mapping.fromJS(json.Layout, mapping));
精彩评论