Knockout.js 为什么在durandal中编写时通过breeze查询时会出现类型错误?
当我的视图和模型被使用durandal的compose绑定语句绑定时,我从breeze和q.js得到以下错误。我单独编写了这个有问题的视图,它工作得很好,直到我尝试用它进行组合绑定,它抛出了这个错误。我尝试将所有自定义属性从实体构造函数移动到初始值设定项,并推迟计算属性的求值,但这并没有阻止错误的发生。我不确定是哪个框架导致了这个问题,或者我自己的代码是否导致了这个问题。我在datacontext中使用了延迟的Q承诺,但我对其进行了注释以排除它,然后返回到使用标准回调函数 错误:Knockout.js 为什么在durandal中编写时通过breeze查询时会出现类型错误?,knockout.js,breeze,durandal,q,Knockout.js,Breeze,Durandal,Q,当我的视图和模型被使用durandal的compose绑定语句绑定时,我从breeze和q.js得到以下错误。我单独编写了这个有问题的视图,它工作得很好,直到我尝试用它进行组合绑定,它抛出了这个错误。我尝试将所有自定义属性从实体构造函数移动到初始值设定项,并推迟计算属性的求值,但这并没有阻止错误的发生。我不确定是哪个框架导致了这个问题,或者我自己的代码是否导致了这个问题。我在datacontext中使用了延迟的Q承诺,但我对其进行了注释以排除它,然后返回到使用标准回调函数 错误: [Q] Unh
[Q] Unhandled rejection reasons (should be empty): [TypeError]
Object [object Object] has no method 'isExpanded'
TypeError: Object [object Object] has no method 'isExpanded'? at domainModel.<anonymous> (http://myApp/App/viewmodels/categoryTreeView.js?v=1.0.0.0:31:59)? at evaluateImmediate (http://myApp/Scripts/knockout-2.2.1.debug.js:1241:41)? at Object.ko.dependentObservable (http://myApp/Scripts/knockout-2.2.1.debug.js:1318:9)? at dmInitializer (http://myApp/App/viewmodels/categoryTreeView.js?v=1.0.0.0:30:29)? at proto._postInitialize (http://myApp/scripts/breeze.debug.js:2975:13)? at mergeEntity (http://myApp/scripts/breeze.debug.js:12768:39)? at processMeta (http://myApp/scripts/breeze.debug.js:12685:24)? at visitAndMerge (http://myApp/scripts/breeze.debug.js:12665:16)? at http://myApp/scripts/breeze.debug.js:12972:20? at Array.map (native)?From previous event:? at executeQueryCore (http://myApp/scripts/breeze.debug.js:12593:77)? at proto.executeQuery (http://myApp/scripts/breeze.debug.js:11461:23)? at Object.getCategories (http://myApp/App/services/datacontext.js?v=1.0.0.0:51:17)? at viewModel.self.getCategories (http://myApp/App/viewmodels/categoryTreeView.js?v=1.0.0.0:204:17)? at init (http://myApp/App/viewmodels/categoryTreeView.js?v=1.0.0.0:223:18)? at new viewModel (http://myApp/App/viewmodels/categoryTreeView.js?v=1.0.0.0:225:9)? at Object.<anonymous> (http://myApp/App/durandal/composition.js?v=1.0.0.0:317:42)
categorytreeview.html
<div id="categoryTreeView">
<script id="recursiveTemplate" type="text/html">
<ul data-bind="visible: isVisible">
<li data-bind="css: { branch: isBranch, leaf: !isBranch(), expanded: isExpanded }">
<div data-bind="click: toggleIsExpanded"></div>
<div>
<input type="checkbox" data-bind="attr: { id: 'c-' + CategoryId() }, value: CategoryId" />
<label data-bind="text: Name, attr: { for: 'c-' + CategoryId() }"></label>
</div>
</li>
<!-- ko template: { name: 'recursiveTemplate', foreach: Children } --><!-- /ko -->
</ul>
</script>
<div class="css-treeview">
<!-- ko template: { name: 'recursiveTemplate', foreach: categories } --><!-- /ko -->
<div id="dialog-buttons">
<input type="button"
value="Cancel" class="btn-cancel">
<input type="button" value="Save" class="btn-primary pull-right">
</div>
</div>
</div>
更新1:
我已经排除了Q.js的问题,它只是堆栈中的最后一个家伙。如果我给我的categoryMapModel()
分配一个初始值,如下所示,一切正常。如果我然后单击一个UI按钮删除并重新显示这个视图,那么一切都会正常工作。如果我也等待并在durandal再次调用的activate()
方法中设置此变量,那么一切正常
self.categoryMapModel = ko.observable("viewmodels/categoryTreeView");
然而,这个变量是在运行时设置的,当用户单击一个按钮时,我就得到了错误。我看到的是breeze.js没有将初始化函数中的自定义属性附加到特定实体。正如您从下面的日志中看到的,当它点击名为“DVD”的类别实体时,它会爆炸出来。由于未知原因,此实体不包含初始化函数中定义的任何自定义属性,如isExpanded()
或isRoot()
在所有实体中,这是唯一一个没有自定义属性的实体。列表中有其他实体紧随其后,它们很好。我不明白为什么breeze在这种情况下会对特定实体产生问题,但事实确实如此
我到底做错了什么/什么会导致breeze.js忽略初始化函数
name: Apparel categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Art categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Books categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Electronics categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Movies categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: root node categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Blu-ray categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
*name: DVD categoryTreeView.js:269
*properties: entityAspect, IsDeleted, CategoryId, PictureId, Name, Description, IsPublished, ParentCategoryId, CreatedOn, ParentCategory, Picture, Children, AdvertiserCategories, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: reddisc categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Animation categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Accessories categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test2 categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test23 categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
rootCategories needed
[Q] Unhandled rejection reasons (should be empty): [TypeError]
我终于明白发生了什么事!在主视图中,我有一个breeze查询,它向下扩展到
类别
表,然后我加载了另一个视图(categoryTreeView),该视图也需要查询类别
表。由于主视图是首先加载的,并且在该视图中没有对类别
实体调用registerEntityTypeCtor
,因此breeze本质上是按原样缓存第一个查询中的任何实体。当我的第二个视图出现时,即使我在其中调用了registerEntityTypeCtor
,它也不会为已经从主视图的查询中加载的实体触发。categoryMapModel.html看起来怎么样categoryMapModel
返回viewModel,但是isExpanded
仅在domainModel
上定义。我将categorytreeview.html添加到文章中。在Durandal视图中定义淘汰模板将不起作用。有关详细信息,请参阅。已将模板移到durandal之外,但仍会出现相同的错误。请注意,如果我在主视图模型中设置categoryMapModel(“viewmodels/categoryTreeView”)
作为初始值,则compose绑定将起作用,但我需要在运行时设置此属性,而此时会发生错误。这就好像有一个时间问题,一个资源没有被提取。
function getCategories(afterGetCategories) {
//var deferred = Q.defer();
var query = breeze.EntityQuery.from("Category")
.orderBy("ParentCategoryId, Name")
.expand("Children");
manager.executeQuery(query)
.then(function (data) {
console.log("callback for getCategories");
//deferred.resolve(data);
afterGetCategories(data);
})
.fail(function (error) { console.log("Error: " + error); });
//return deferred.promise;
};
self.categoryMapModel = ko.observable("viewmodels/categoryTreeView");
name: Apparel categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Art categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Books categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Electronics categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Movies categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: root node categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Blu-ray categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
*name: DVD categoryTreeView.js:269
*properties: entityAspect, IsDeleted, CategoryId, PictureId, Name, Description, IsPublished, ParentCategoryId, CreatedOn, ParentCategory, Picture, Children, AdvertiserCategories, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: reddisc categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Animation categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: Accessories categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test2 categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test23 categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
name: test categoryTreeView.js:269
properties: isSelected, isExpanded, toggleIsExpanded, toggleIsSelected, entityAspect, CreatedOn, CategoryId, PictureId, Name, Description, IsPublished, IsDeleted, ParentCategoryId, Children, Picture, ParentCategory, AdvertiserCategories, isBranch, isRoot, isVisible, path, _$typeName, entityType, _$interceptor, getProperty, setProperty, categoryTreeView.js:270
rootCategories needed
[Q] Unhandled rejection reasons (should be empty): [TypeError]