代码之家  ›  专栏  ›  技术社区  ›  sksallaj

BreezeJS编辑数据不工作

  •  0
  • sksallaj  · 技术社区  · 10 年前

    我能够按照说明添加数据,这部分很容易理解。但当我试图按照指示编辑数据时,我完全迷失了方向。

    我遵循todo示例,它工作得很好,但当我试图使用相同的原理添加到我自己的项目中时,没有任何效果。

    在我的控制器中,我有以下内容:

    function listenForPropertyChanged() {
        // Listen for property change of ANY entity so we can (optionally) save
        var token = dataservice.addPropertyChangeHandler(propertyChanged);
    
        // Arrange to remove the handler when the controller is destroyed
        // which won't happen in this app but would in a multi-page app
        $scope.$on("$destroy", function () {
            dataservice.removePropertyChangeHandler(token);
        });
    
        function propertyChanged(changeArgs) {
            // propertyChanged triggers save attempt UNLESS the property is the 'Id'
            // because THEN the change is actually the post-save Id-fixup 
            // rather than user data entry so there is actually nothing to save.
            if (changeArgs.args.propertyName !== 'Id') { save(); }
        }
    }
    

    问题是,每当我更改视图上的控件时,都不会调用propertyChanged回调函数。

    这是服务的代码:

    function addPropertyChangeHandler(handler) {
        // Actually adds any 'entityChanged' event handler
        // call handler when an entity property of any entity changes
        return manager.entityChanged.subscribe(function (changeArgs) {
            var action = changeArgs.entityAction;
            if (action === breeze.EntityAction.PropertyChange) {
                handler(changeArgs);
            }
        });
    }
    

    如果我在线路上设置断点:

    var action = changeArgs.entityAction;
    

    在我的项目中,它从未到达那里;在todo示例中,确实如此!它完全跳过整个过程,然后加载视图。因此,我的回调函数都不起作用;因此,实际上,没有订阅任何内容。

    因此,当我尝试保存更改时,manager.hasChanges()始终为false,数据库中不会发生任何变化。

    我已经试了至少3天让它生效,我完全被整个问题对我来说是多么复杂而目瞪口呆。

    注意:我使用的是Johnpa的HotTowel模板。我试图将Todo编辑功能应用于T恤。。没有任何事情能按照我希望的方式进行。

    如有帮助,将不胜感激。

    1 回复  |  直到 10 年前
        1
  •  0
  •   sksallaj    10 年前

    我一直认为问题出在javascript客户端。事实证明,当您创建投影DTO时,编辑不起作用。

    所以在我的服务器端,我创建了一个查询:

    public IQueryable<PersonDTO> getPerson(){
         return (from _person in ContextProvider.Context.Queries
                select new PersonDTO
                {
                    Id = _person.Id,
                    FirstName = _person.FirstName,
                    LastName = _person.LastName
                }).AsQueryable();
    }
    

    它刚刚向客户发送了一个DTO。这在我的应用程序中确实在获取数据和填充数据方面发挥了作用。所以这并没有错。使用这个,我可以添加项目和获取项目,但没有任何信息允许实体管理员了解项目。当我创建一个项目时,entitymanager有一个“createEntity”,允许我告诉entitymanager要使用哪个项目。。在我的情况下:

    manager.createEntity(person, initializeValues);
    

    也许如果有一个“manager.getEntity”也许会有所帮助?

    无论如何,我更改了上面的查询,以直接从源代码获取:

    public IQueryable<Person> getPeople(){
        return ContextProvider.Context.People;
    }
    

    注意ContextProvider是:

    readonly EFContextProvider<PeopleEntities> ContextProvider = 
        new EFContextProvider<PeopleEntities>();
    

    因此javascript中的subscribe方法检查直接从上下文对象检索的信息。。有趣的真希望我没有花4天时间在这上面。