0

我正在使用 Knockout.js 2.2.1 和 Breeze 1.3.5 来检索和显示一些数据,但结果很差。数据来自自引用表。据我所知,Knockout 似乎认为每个项目的Children属性应该返回整个数组,而不是项目的实际子项。我究竟做错了什么?

更新1:我创建了一个小提琴,我得到了预期的结果,但它不使用微风.js。所以也许这是一个微风.js 问题?小提琴中值得注意的是,ChildrenName都在没有括号的情况下被引用,而在我完整的应用程序中,微风.js 似乎正在将它们转换为正确的 observables* 并且需要Name的括号。

当前结果:

Apparel
- Apparel
Art
- Art
Books
- Books
Movies
- Movies
test
- test
Blu-ray
- Blu-ray
DVD
- DVD
Animation
- Animation
Accessories
- Accessories

预期结果:

Apparel
- Accessories
Art
- Animation
Books
Movies
- Blu-ray
- DVD
test

虚拟机:

var vm = {
    categories: ko.observableArray()
};

Knockout.js 标记:

<div data-bind="foreach: categories">
    <div data-bind="text: Name"></div>
    <div data-bind="foreach: Children">
        <div data-bind="text: ' - ' + Name()"></div>
    </div>
</div>

微风代码:

var query = breeze.EntityQuery.from("Category")
    .orderBy("ParentCategoryId, Name")
    .expand("Children");

_entityManager
    .executeQuery(query)
    .then(function (data) {
        vm.categories.removeAll();
        console.log("here: " + data.results.length); -- produces 9
        data.results.forEach(function (item) {
            vm.categories.push(item);
        });
    })
    .fail(function (error) { console.log("Error: " + error); });

微风查询结果:

[ { "$id" : "1",
    "$type" : "SR.Data.Models.Domain.Category, SR.Data",
    "CategoryId" : 5,
    "Children" : [ { "$id" : "2",
          "$type" : "SR.Data.Models.Domain.Category, SR.Data",
          "CategoryId" : 6,
          "Children" : [  ],
          "CreatedOn" : "2013-05-31T11:29:11.860",
          "Description" : "Accessories",
          "IsDeleted" : false,
          "IsPublished" : true,
          "Name" : "Accessories",
          "ParentCategory" : { "$ref" : "1" },
          "ParentCategoryId" : 5
        } ],
    "CreatedOn" : "2013-05-31T11:28:37.677",
    "Description" : "Apperal",
    "IsDeleted" : false,
    "IsPublished" : true,
    "Name" : "Apparel"
  },
  { "$id" : "3",
    "$type" : "SR.Data.Models.Domain.Category, SR.Data",
    "CategoryId" : 3,
    "Children" : [ { "$id" : "4",
          "$type" : "SR.Data.Models.Domain.Category, SR.Data",
          "CategoryId" : 4,
          "Children" : [  ],
          "CreatedOn" : "2013-05-31T11:25:46.333",
          "Description" : "Animation",
          "IsDeleted" : false,
          "IsPublished" : true,
          "Name" : "Animation",
          "ParentCategory" : { "$ref" : "3" },
          "ParentCategoryId" : 3
        } ],
    "CreatedOn" : "2013-05-31T11:25:30.250",
    "Description" : "Art",
    "IsDeleted" : false,
    "IsPublished" : true,
    "Name" : "Art"
  },
  { "$id" : "5",
    "$type" : "SR.Data.Models.Domain.Category, SR.Data",
    "CategoryId" : 1,
    "Children" : [  ],
    "CreatedOn" : "2013-05-13T17:14:15.880",
    "Description" : "Books",
    "IsDeleted" : false,
    "IsPublished" : true,
    "Name" : "Books"
  },
  { "$id" : "6",
    "$type" : "SR.Data.Models.Domain.Category, SR.Data",
    "CategoryId" : 2,
    "Children" : [ { "$id" : "7",
          "$type" : "SR.Data.Models.Domain.Category, SR.Data",
          "CategoryId" : 7,
          "Children" : [  ],
          "CreatedOn" : "2013-05-31T11:31:05.710",
          "Description" : "Blu-ray",
          "IsDeleted" : false,
          "IsPublished" : true,
          "Name" : "Blu-ray",
          "ParentCategory" : { "$ref" : "6" },
          "ParentCategoryId" : 2
        },
        { "$id" : "8",
          "$type" : "SR.Data.Models.Domain.Category, SR.Data",
          "CategoryId" : 8,
          "Children" : [  ],
          "CreatedOn" : "2013-05-31T11:31:15.000",
          "Description" : "DVD",
          "IsDeleted" : false,
          "IsPublished" : true,
          "Name" : "DVD",
          "ParentCategory" : { "$ref" : "6" },
          "ParentCategoryId" : 2
        }
      ],
    "CreatedOn" : "2013-05-14T12:13:36.570",
    "Description" : "DVD's & Blu-ray",
    "IsDeleted" : false,
    "IsPublished" : true,
    "Name" : "Movies"
  },
  { "$id" : "9",
    "$type" : "SR.Data.Models.Domain.Category, SR.Data",
    "CategoryId" : 11,
    "Children" : [  ],
    "CreatedOn" : "2013-06-03T15:32:50.023",
    "Description" : "test",
    "IsDeleted" : false,
    "IsPublished" : true,
    "Name" : "test"
  },
  { "$ref" : "7" },
  { "$ref" : "8" },
  { "$ref" : "4" },
  { "$ref" : "2" }
]

类别实体:

public class Category
{
    public Category() { }

    public virtual int CategoryId { get; set; }

    public virtual int? ParentCategoryId { get; set; }

    public virtual int? PictureId { get; set; }

    public virtual string Name { get; set; }

    public virtual string Description { get; set; }

    public virtual bool IsPublished { get; set; }

    public virtual bool IsDeleted { get; set; }

    public virtual DateTime CreatedOn { get; set; }

    public virtual Category ParentCategory { get; set; }

    public virtual Picture Picture { get; set; }

    public virtual ICollection<Category> Children { get; set; }
}

类别映射:

modelBuilder.Entity<Category>().HasKey(a => a.CategoryId);
modelBuilder.Entity<Category>().HasOptional(x => x.Picture).WithMany().HasForeignKey(x => x.PictureId);
modelBuilder.Entity<Category>().HasOptional(x => x.ParentCategory).WithMany().HasForeignKey(x => x.ParentCategoryId);
modelBuilder.Entity<Category>().HasMany(x => x.Children).WithOptional().HasForeignKey(x => x.ParentCategoryId);

类别表:

类别表

4

2 回答 2

0

终于想通了!我的实体映射设置错误。就在我认为我不能再讨厌制作这个令人费解的 api 的人时,他们证明我错了。

modelBuilder.Entity<Category>().HasOptional(x => x.ParentCategory).WithMany(x => x.ChildrenElements).HasForeignKey(x => x.ParentCategoryId);
于 2013-06-09T21:15:46.737 回答
0

说不出来。看起来基本正确..除了标记错误(下)。JSON 很好,但在查询完成后,我不知道您对 Category 实体实际拥有什么。

在我们进行下一步之前,修复标记时会发生什么?

标记错误

您不希望子模板中“名称”后面的括号。它应该是

<div data-bind="text: ' - ' + Name"></div> <!-- NO PARENS AFTER NAME -->

就像在你的小提琴中一样。

清理成功回调

当您在这里时,您可能会考虑这种替代语法,它只是将内部数组替换为查询结果数组;不需要迭代。

.then(函数(数据){
        vm.categories(data.results);
        console.log("这里:" + data.results.length); -- 产生 9
        });
    })
于 2013-06-08T07:20:17.210 回答