Javascript - Breeze Manager未检测到更改

时间:2014-11-26 18:05:46

标签: javascript c# breeze entitymanager

我正在使用Breeze的EntityManager来获取数据绑定的API部分。但是,EntityManager无法跟踪更改。它将像它应该的那样执行代码,但它永远不会识别出这些变化。有什么问题?请不要说任何不具有建设性或任何人身攻击的内容。我们在这里是专业人士和科学家(我知道)。这是我的代码:

服务:

(function () {
var serviceId = 'UWRLService';
angular.module('myApp')
    .factory(serviceId, ['$q', 'breeze', 'logger', 'appSettings', UWRLService]);
// console.log('Initialized UWRL Service.js');
function UWRLService($q, breeze, logger, appSettings) {
   // console.log('inside datacontext -- UWRLService');
    // configure logging for this service
    logger = logger.forSource(serviceId);
    var logError = logger.logError;
    var logSuccess = logger.logSuccess;
    var logWarning = logger.logWarning;
    //Setup variables with common Breeze query classes
    var entityQuery = breeze.EntityQuery;

    // setup breeze entity manager       
    var serviceName = appSettings.apiUrl + '/breeze/Uwrl/';//Where the entire service is pointing to

    var manager = new breeze.EntityManager(serviceName);
    var entityStateChangeAction = breeze.EntityAction.EntityStateChange;


    // expose methods 
    var service = {
        getChangesCount: getChangesCount,
        saveChanges: saveChanges, 
        rejectChanges: rejectChanges,
        getDivisions: getDivisions,
        getPools: getPools,
        getRandomCust: getRandomCust
        //createChangeFactorEntity: createChangeFactorEntity,
    };
    return service;

    //           FUNCTION DECLARATIONS
    //Attaches a new entity to the Breeze repository 
    //Passes the name and an array of values to seed the entity with
    //function createChangeFactorEntity(entityName, initialValues) {
    //    var newFactor = manager.createEntity(entityName, initialValues);
    //    return newFactor;
    //}

    function getRandomCust()
    {
        var query = breeze.EntityQuery.from('alpha')
                        .where('customerNumber', '==', 1);
        return executeQuery(query, 'Alpha found!');
    }

    function getDivisions()
    {
        var query = breeze.EntityQuery
            .from('Divisions');

            //executeQuery([query name], [query title])
        return executeQuery(query, 'Divisions Found');
    }

    function getPools()
    {
        var query = breeze.EntityQuery
                    .from('Pools');
        return executeQuery(query, 'Pools Found');
    }

    //Saves changes and logs exceptions
    function saveChanges() {
        var hasChanges = manager.hasChanges();
        console.log(hasChanges);
        console.log(manager.getChanges());

        return manager.saveChanges()
            .then(saveSucceeded)
            .catch(saveFailed);

        function saveSucceeded(saveResult) {
            logSuccess("# of items saved = " + saveResult.entities.length, null, true);
            logger.log(saveResult);
        }

        function saveFailed(error) {
            var reason = error.message;
            var detail = error.detail;

            if (error.entityErrors) {
                //Do nothing
            } else if (detail && detail.ExceptionType &&
                detail.ExceptionType.indexOf('OptimisticConcurrencyException') !== -1) {
                // Concurrency error 
                reason =
                    "Another user, perhaps the server, " +
                    "may have deleted one or all of the todos." +
                    " You may have to restart the app.";
            } else {
                reason = "Failed to save changes: " + reason +
                    " You may have to restart the app.";
            }

            logError(reason, error, true);

            throw error; //Downstream: users know it has failed
        }
    }

    //Discards changes in Breeze Manager
    function rejectChanges() {
        if (manager.hasChanges()) {
            count = getChangesCount();
            manager.rejectChanges();
            logWarning('Discarded ' + count + ' pending changes(s)', null, true);
        }
    }

    //Returns (Nth-1) index of Breeze manager getChanges array
    function getChangesCount() {
        var ents = manager.getEntities();
        var changes = manager.getChanges();
        if (changes.length > 0)
        {
            alert("Changes made: " + manager.getChanges().length);
        }
        return manager.getChanges().length;
    }

    //Query Execution w/ toasters(logger)
    function executeQuery(query, entityType) {

        var promise = manager.executeQuery(query).then(querySucceeded, queryFailed);
        return promise;

        function querySucceeded(response) {
            logSuccess(entityType + " query was successful", null, true);

            return response.results;
        }

        function queryFailed(response) {
            var message = response.message || entityType + " query failed";
            logError(message, response, true);
            throw error;
        }
    }
};
})()

控制器(javascript):

(function () {
'use strict';
var controllerId = 'UWRLController';
// console.log('Initialized UWRLController');
//Last item in passed array is the Controller (specific)
angular.module('myApp').controller(controllerId, 
['$scope', 'UWRLService', 'logger',                                                                             
'$routeParams', 'allStatesService', UWRLController]);

function UWRLController($scope, UWRLService, logger, $routeParams, allStatesService) {
   // console.log('inside UWRLController');
    //Loggin Initialization       
    logger = logger.forSource(controllerId);
    var logError = logger.logError;
    var logSuccess = logger.logSuccess;
    var logWarning = logger.logWarning;

    var uwrl = {};
    $scope.uwrl = uwrl;
    //Parameters we pass from Renewal Group Maintenance screen
    //uwrl.PlanCode = $routeParams.PlanCode;
    //uwrl.Contract = $routeParams.ContractNumber;
    //uwrl.Mch = $routeParams.Mch;

    //Functions in Javascript Controller
    //[scope].[property] = [function name]
    uwrl.saveChanges = save;
    uwrl.discardChanges = discardChanges;
    uwrl.changesCount = changesCount();
    //uwrl.select = select;
    init();//Initialize all customer related data for page

    function init()
    {
        gettingDivisions();//Initialze getting data from Division's table through UWRL-service.js
        getAllFiftyStates();
        gettingPools();
        gettingRandom();
    }

    function gettingRandom()
    {
        UWRLService.getRandomCust()
                .then(function(alpha)
                {
                    uwrl.alpha = alpha;
                    uwrl.beta = uwrl.alpha[0].customerName;
                });
    }

    function gettingDivisions()
    {
        UWRLService.getDivisions()
            .then(function (divisionNumber) {
                uwrl.divisionNumber = divisionNumber;
            });
    }

    function getAllFiftyStates()
    {
        allStatesService.getStates()
            .then(function (allStates)
            {
                uwrl.allStates = allStates;
            });
    }

    function gettingPools()
    {          
        UWRLService.getPools()
            .then(function (poolNumber)
            {
                uwrl.poolNumber = poolNumber;
            });
    }

    //Clicking the Drop-down Button
    //function select(change) {
    //    this.MchMcpPlanDesignId = change.MchMcpPlanDesign.MchMcpPlanDesignId;
    //    change.expanded = !change.expanded; //toggle back and forth           
    //}


    ////.then = [if] success
    ////.fail = failure
    ////.finally = always executed despite evaluated conditionals        
    //function getPlans() {//returns a promise
    //    uwrl.loadingPlans = true;
    //    UWRLService.getChangeFactors(uwrl.Mch, uwrl.Contract, uwrl.PlanCode)
    //        .then(function (deltaChangeFactor) {
    //            uwrl.deltaChangeFactor = deltaChangeFactor;
    //        }).finally(function () { uwrl.loadingPlans = false; });
    //}

    ////Returns all data in ChangeFactorType table
    //function getChangeFactorTypes() {
    //    UWRLService.getTypes().then(function (changeFactorTypes) {
    //        uwrl.changeFactorTypes = changeFactorTypes;
    //    });
    //}

    //Clicking on Save Button
    function save() {
        console.log('Save Button Clicked!');
        //Validation -- checks for empty values
        //if (uwrl.changeFactorType != null && uwrl.effectiveDate != null &&
        //   uwrl.changeFactorAmount != null) {
        //    //Adds a new Breeze Entity for ChangeFactor table in SQL database
        //    UWRLService.createChangeFactorEntity('ChangeFactor',
        //    {
        //        MchMcpPlanDesignId: this.MchMcpPlanDesignId,
        //        ChangeFactorType: uwrl.changeFactorType,
        //        EffectiveDate: uwrl.effectiveDate,
        //        ChangeFactorAmount: uwrl.changeFactorAmount
        //    });
        //}
        //Saves to Breeze Manager
        //Must hit Art's ESB service -- to be researched
        UWRLService.saveChanges();
    }

    //Gets rid of changes and logs it
    function discardChanges() {
       console.log('Discard Button Clicked!');
        UWRLService.rejectChanges();
    }

    //Notifies user(s) of changes made that are
    //either: savable, discardable 
    function changesCount() {
       // console.log("Changes Made: " + UWRLService.getChangesCount)//for debugging purposes
        return UWRLService.getChangesCount;
    }
};
})();

1 个答案:

答案 0 :(得分:0)

答案是确保影响模型。例如:uwrl.alpha [0] .customerName而不是urwl.beta