为什么在durandal撰写期间通过微风查询时出现TypeError? [英] Why am I getting a TypeError when querying through breeze during a compose in durandal?

查看:68
本文介绍了为什么在durandal撰写期间通过微风查询时出现TypeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的视图和模型由使用durandal的组合绑定语句绑定时,我从breeze和q.js中收到以下错误.我隔离地编写了有问题的视图,并且在我尝试对其进行组合绑定之前,它一直很好用,它会引发此错误.我尝试将所有自定义属性从我的实体构造函数移到初始化程序,还推迟了对我的计算函数的评估,但这并不能阻止该错误.我不确定哪个框架会导致此问题,或者我自己的代码是否会导致此问题.我在数据上下文中使用了递延的Q Promise,但我对其进行了评论以将其排除在外,然后返回使用标准的回调函数.

I'm getting the following error from breeze and q.js when my view and model are being bound by a compose binding statement using durandal. I wrote the view in question in isolation and it works great until I try to do a compose binding with it, it throws this error. I tried moving all the custom properties from my entity constructor function to the initializer and also defering evalution of my computed's but that did nothing to thwart the error. I'm not sure which framework is causing this problem or if my own code is. I was using a deferred Q promise in my datacontext but I commented it to rule it out and went back to using a standard callback function.

错误:

[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.js:

define(["services/datacontext"], function (ctx) {
    function domainModel() {
        var self = this;
        self.isSelected = ko.observable(false);
        self.isExpanded = ko.observable(false);

        self.toggleIsExpanded = function () {
            self.isExpanded(!self.isExpanded());
        };
        self.toggleIsSelected = function () {
            self.isSelected(!self.isSelected());
        };
    }
    var dmInitializer = function (item) {
        item.isBranch = ko.computed(function () {
            return item.Children().length > 0 || item.ParentCategory() == null;
        });
        item.isRoot = ko.computed(function () {
            return item.ParentCategory() == null;
        });
        item.isVisible = ko.computed(function() {
            return item.isRoot() || item.ParentCategory().isExpanded();
        }, item);
    };

    ctx.metadataStore.registerEntityTypeCtor("Category", domainModel, dmInitializer);

    function viewModel() {
        var self = this;
        self.categories = ko.observableArray();

        self.getCategoriesCallback = function (data) {
            console.log("callback for getCategories");
            self.categories(data.results);
        };
        self.getCategories = function () {
            ctx.getCategories(self.getCategoriesCallback);
        };
        init = function () {
            self.getCategories();
        };
        init();
    }
    return viewModel;
}

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>

这是我正在撰写失败的视图时从主视图获取的html:

<div data-bind="compose: { model: categoryMapModel, activate: true, cacheViews: false }">
</div>

datacontext.js

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;
};

更新1:

我排除了Q.js的问题,它只是堆栈中的最后一个家伙.如果我为我的categoryMapModel()分配一个如下所示的初始值,则一切正常.如果然后我单击一个UI按钮,该按钮将删除并重新显示此视图,则一切正常.如果我也等待并在durandal再次调用的activate()方法中设置此变量,则一切正常.

I’ve ruled Q.js out as a problem, it’s just the last guy in stack. If I assign my categoryMapModel() with an initial value like below everything works fine. If I then click a UI button that removes and re-shows this view, again everything works fine. If I also wait and set this variable in the activate() method that durandal calls again everything works just fine.

self.categoryMapModel = ko.observable("viewmodels/categoryTreeView");

但是,在用户单击按钮时(即当我收到错误消息时),会在运行时设置此变量.我看到的是breeze.js并未将自定义属性从初始化函数附加到特定实体.从下面的日志中可以看到,当它命中名为"DVD"的类别实体时,它会爆炸.出于未知原因,该实体不包含初始化函数中定义的任何自定义属性,例如isExpanded()isRoot(). 在所有实体中,这是唯一没有自定义属性的实体.列表中还有其他实体,它们都很好.我看不出有什么逻辑可以解释为什么在这种情况下微风对特定实体会有问题,但是确实如此.

However this variable is set during run-time when the user clicks a button and that’s when I get the error. What I’m seeing is that breeze.js is not appending the custom properties from my initialization function to specific entities. As you can see from the logging below when it hits the category entity named "DVD" it bombs out. This entity, for unknown reasons, does not contain any of the custom properties like isExpanded() or isRoot() that is defined in the initialization function. Out of all the entities this is the only one that doesn't have the custom properties. There are other entities that come after it in the list and they are fine. I don’t see any logic why breeze would have an issue with a specific entity in this scenario but it is.

我到底在做什么错/什么会导致breeze.js忽略初始化函数?

What in the world am I doing wrong/What would cause breeze.js to ignore the initialization function?

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]

推荐答案

我终于知道发生了什么!在主视图中,我有一个轻而易举的查询,它扩展到了Category表,然后又加载了另一个查询Category表所需的视图(categoryTreeView).因为主视图首先加载,并且在该视图中没有调用Category实体,所以微风实际上是按原样缓存该第一个查询中的所有实体.当我出现第二个视图时,即使我确实在其中调用registerEntityTypeCtor,对于已从主视图查询中加载的实体也不会触发.

I finally see what's going on! In the main view I have a breeze query that expanded down to the Category table and I was then loading another view (categoryTreeView) that also needed to query the Category table. Since the main view loaded first and there was no call to registerEntityTypeCtor in that view for the Category entity breeze is essentially caching any entities from that first query as-is. When my second view comes along, and even though I do call registerEntityTypeCtor in there, it doesn't fire for the entities that were already loaded from the main view's query.

这篇关于为什么在durandal撰写期间通过微风查询时出现TypeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆