0

我使用 json 对象来处理我的菜单和面包屑。现在,您可以在下面看到前两个“导航”节点是可观察的,但最后一个不是。由于某种原因,它只是一个常规数组。映射插件不会深度克隆对象吗?

萤火虫输出:

app.viewModel.members.layout().Navigation()[2].Navigation()[1].Navigation() <-- errors

初始化:

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"
            }
          }
        ]
      }
    ]
  }
}

更新:

仔细看,因为“导航”只有一个节点,所以它使它成为一个对象,而不是像其他对象那样的数组。我该如何补救?在映射插件中使用创建?

4

1 回答 1

1

好吧,我在映射插件中使用了 create 方法。

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));
于 2011-09-27T05:07:24.687 回答