简体   繁体   English

微风1-m-1在HotTowel Angular中具有本地存储

[英]Breeze 1-m-1 in HotTowel Angular with local storage

I've had a requirement recently to implement a UI for managing a many-many relationship. 我最近有一个需求,要求实现一个用于管理多对多关系的UI。 Ward Bell kindly provided this plunker showing how to implement using 1-m-1 with Angular and Breeze. 沃德·贝尔(Ward Bell)友善地提供了这个小家伙,展示了如何在Angular和Breeze中使用1-m-1来实现。

My app's design is based largely (especially the datacontext and the local storage) is based largely on John Papa's recent Pluralsight courses. 我的应用程序的设计主要基于John Papa最近的Pluralsight课程(尤其是数据上下文和本地存储)。

In my app, BusUnit = Hero and Dimension = Power (in reference to Ward's example. 在我的应用中,BusUnit = Hero,Dimension = Power(参考Ward的示例)。

Everything seems to be working well when I force the app to fetch data from the server, in that my updates to a business unit's dimensions reflect correctly. 当我强制应用从服务器获取数据时,一切似乎都运行良好,因为我对业务部门尺寸的更新正确反映了。 The problem I'm facing now is when I navigate away from the page and back again (which gets data from local storage). 我现在面临的问题是当我离开页面并再次返回(从本地存储获取数据)时。 In this case: 在这种情况下:

  • if I previously added a new dimension to a business unit, everything is ok, but 如果我以前向业务部门添加了新的维度,一切都很好,但是
  • if i previously marked a business unit's dimension for deletion and the save, the dimension still appears for the business unit in question. 如果我先前将业务部门的维度标记为要删除和保存,则该业务部门的维度仍会出现。

this is the controller code that initially gets business units and their dimensions: 这是最初获取业务部门及其规模的控制器代码:

function getdboardStructure() {
        var busUnitsPromise = datacontextSvc.busUnits.getByDboardConfigId(vm.dboardConfig.id);
        var dimensionsPromise = datacontextSvc.dimensions.getByDboardConfigId(vm.dboardConfig.id);

        $q.all([busUnitsPromise, dimensionsPromise])
            .then(function (values) {
                vm.busUnits = values[0];
                vm.dims = values[1];
                createBusUnitVms();
                //vm.currentBusUnitVm = vm.busUnitVms[0]; // not required as using accordion instead of drop-down
                vm.hasChanges = false;
            });
    }

this is the code in my controller that prepares for the save: 这是控制器中准备保存的代码:

function applyBusUnitDimensionSelections(busUnitVm) {
        var busUnit = busUnitVm.busUnit;
        var mapVms = busUnitVm.dimensionMapVms;
        var dimensionHash = createBusUnitDimensionHash(busUnit);

        mapVms.forEach(function (mapVm) {
            var map = dimensionHash[mapVm.dimension.id];

            if (mapVm.selected) {
                if (!map) {
                    datacontextSvc.busUnits.addBusUnitDimension(busUnit, mapVm.dimension)
                    .then(function () {
                    });
                }
            } else {
                if (map) {
                    datacontextSvc.markDeleted(map);
                }
            }
        });
    }

this is the code in my controller that executes the save: 这是执行保存的控制器中的代码:

function save() {
        if (!canSave()) {
            return $q.when(null);
        }

        vm.isSaving = true;
        vm.busUnitVms.forEach(applyBusUnitDimensionSelections);
        return datacontextSvc.save().then(function (saveResult) {
            vm.isSaving = false;
            trapSavedDboardConfigId(saveResult); // not relevant to use case
        }, function (error) {
            vm.isSaving = false;
        });
    }

this is the code in my repository that add a new busUnitDimension entity: 这是我的存储库中添加新的busUnitDimension实体的代码:

function addBusUnitDimension(busUnit, dimension) {
        var newBusUnitDimension = this.em.createEntity(busUnitDimension);
        newBusUnitDimension.busUnitId = busUnit.id;
        newBusUnitDimension.dimensionId = dimension.id;
        return this.$q.when(newBusUnitDimension);
    }

this is my datacontext code for marking an item deleted: 这是我的数据上下文代码,用于标记项目已删除:

function markDeleted(entity) {
        return entity.entityAspect.setDeleted();
    } 

and finally this is the repository code to get business units and their join table entities: 最后,这是获取业务部门及其联接表实体的存储库代码:

function getByDboardConfigId(dboardConfigId, forceRefresh) {
        var self = this;
        var predicate = pred.create('dboardConfigId', '==', dboardConfigId);
        var busUnits;

        if (self.zStorage.areItemsLoaded('busUnits') && !forceRefresh) {
            busUnits = self._getAllLocal(entityName, orderBy, predicate);
            return self.$q.when(busUnits);
        }

        return eq.from('BusUnits')
            .expand('BusUnitDimensions')
            .where(predicate)
            .orderBy(orderBy)
            .using(self.em).execute()
            .to$q(succeeded, self._failed);

        function succeeded(data) {
            busUnits = data.results;
            self.zStorage.areItemsLoaded('busUnits', true);

            self.zStorage.save();
            //self.logSuccess('Retrieved ' + busUnits.length + ' business units from server', busUnits.length, true);
            return busUnits;
        }
    }

My departure from John's course examples is that I'm using expand in the function I use to get Business Units from the server, and my hypothesis is that this has something to do with the fact that breeze is going to the server everytime I refresh the page (without clearing cache) instead, and that this also has something to do with the error i'm receiving if I navigate away and then back to the page. 我与John的课程示例的不同之处在于,我正在使用在我用于从服务器获取业务单位的功能中使用expand,并且我的假设是,这与每次刷新服务器时都会向服务器发送微风有关页面(不清除缓存),这也与我离开并返回页面时收到的错误有关。

Can anyone offer and suggestions? 谁能提供和建议?

Appreciate this was a long time ago and you have probably solved it or moved on but I came up against the same problem recently that took me ages to resolve. 意识到这是很久以前的事了,您可能已经解决了或者继续前进,但是最近我遇到了同样的问题,这使我花了很长时间来解决。 The answer I found is that you have to edit JP's angular.breeze.storagewip.js file. 我发现的答案是,您必须编辑JP的angular.breeze.storagewip.js文件。 I contains the names of the entities hard-coded into the file and you will need to change these to match your own entities. 我包含硬编码到文件中的实体名称,您将需要更改它们以匹配您自己的实体。

There are two functions where you need to do this, examples below show the changes with the four entities I am using: 您需要执行两个功能,以下示例显示了我正在使用的四个实体的更改:

function zStorageCore($rootScope, zStorageConfig) {
    var storeConfig = zStorageConfig.config;
    var storeMeta = {
        breezeVersion: breeze.version,
        appVersion: storeConfig.version,
        isLoaded: {
          elementAssets : false,
          surveyors : false,
          elements : false,
          assets : false
        }
    };

and... 和...

    function checkStoreImportVersionAndParseData(importedData) {
        if (!importedData) {
            return importedData;
        }
        try {
            var data = JSON.parse(importedData);
            var importMeta = data[0];
            if (importMeta.breezeVersion === storeMeta.breezeVersion &&
                importMeta.appVersion === storeMeta.appVersion) {
              if (importMeta.isLoaded) {
                storeMeta.isLoaded.assets = storeMeta.isLoaded.assets || importMeta.isLoaded.assets;
                storeMeta.isLoaded.elements = storeMeta.isLoaded.elements || importMeta.isLoaded.elements;
                storeMeta.isLoaded.surveyors = storeMeta.isLoaded.surveyors || importMeta.isLoaded.surveyors;
                storeMeta.isLoaded.elementAssets = storeMeta.isLoaded.elementAssets || importMeta.isLoaded.elementAssets;
              }
                return data[1];
            } else {
                _broadcast(storeConfig.events.error,
                    'Did not load from storage because mismatched versions',
                    { current: storeMeta, storage: importMeta });
            }
        } catch (ex) {
            _broadcast(storeConfig.events.error, 'Exception during load from storage: ' + ex.message, ex);
        }
        return null; // failed
    }

I solved this by comparing JP's Style Guide course files with his SPA/Angular/Breeze course. 我通过将JP的“风格指南”课程文件与他的SPA / Angular / Breeze课程进行比较来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM