breezejs实体框架角度

时间:2014-08-20 16:55:36

标签: angularjs entity-framework breeze

我在使用最新版本的Breeze Angular客户端查询使用VS2013EF6WebAPI2编写的Breeze服务时遇到问题。

我的服务提供元数据,json到我的端点(url);但是,我似乎在正确获取客户端设置上的dataservice.js文件时遇到问题。只需将服务注入控制器,触发未定义的不是函数错误。

我的代码与原始示例非常接近:

我错过了什么,似乎Breeze应该很容易设置。 EF6BreezeAngularWebAPI2的某处是否有工作样本;但是,不是OData

提前谢谢

//////////////////////
//  dataservice.js  //
//////////////////////

/* dataservice: data access and model management layer 
 * relies on Angular injector to provide:
 *     $timeout - Angular equivalent of 'setTimeout'
 *     breeze - the Breeze.Angular service (which is breeze itself)
 *     logger - the application's logging facility
 */


(function() {

angular.module('app').factory('dataservice', ['$http', '$q', '$timeout', 'breeze', 'logger', dataservice]);

function dataservice( $http, $q, $timeout, breeze, logger) {

    var serviceName = 'breeze/userPortal'; // route to the same origin Web Api controller

    // *** Cross origin service example  ***
    // When data server and application server are in different origins
    //var serviceName = 'http://sampleservice.breezejs.com/api/todos/';

    var manager = new breeze.EntityManager(serviceName);
    manager.enableSaveQueuing(true);

    //REVEALING MODULE
    var service = {
        addPropertyChangeHandler: addPropertyChangeHandler,
        createuser: createuser,
        deleteuser: deleteuser,
        getusers: getusers,
        hasChanges: hasChanges,
        purge: purge,
        reset: reset,
        removePropertyChangeHandler: removePropertyChangeHandler,
        saveChanges: saveChanges
    };

    return service;

    /*** implementation details ***/

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

    function createuser(initialValues) {
        return manager.createEntity('user', initialValues);
    }

    function deleteuser(user) {
        user && user.entityAspect.setDeleted();
    }

    function getusers() {
        var query = breeze.EntityQuery
            .from("users")
            .orderBy("LastName");
    }

    var promise = manager.executeQuery(query).catch(queryFailed);
    return promise;

    function queryFailed(error) {
        logger.error(error.message, "Query failed");
        return $q.reject(error); // so downstream promise users know it failed
    }
});

function hasChanges() {
    return manager.hasChanges();
}

function handleSaveValidationError(error) {
    var message = "Not saved due to validation error";
    try { // fish out the first error
        var firstErr = error.entityErrors[0];
        message += ": " + firstErr.errorMessage;
    } catch (e) { /* eat it for now */ }
    return message;
}

function removePropertyChangeHandler(handler) {
    // Actually removes any 'entityChanged' event handler
    return manager.entityChanged.unsubscribe(handler);
}

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

    function saveSucceeded(saveResult) {
        logger.success("# of users saved = " + saveResult.entities.length);
        logger.log(saveResult);
    }

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

        if (error.entityErrors) {
            reason = handleSaveValidationError(error);
        } 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 users." +
                " You may have to restart the app.";
        } else {
            reason = "Failed to save changes: " + reason +
                " You may have to restart the app.";
        }

        logger.error(error, reason);
        // DEMO ONLY: discard all pending changes
        // Let them see the error for a second before rejecting changes
        $timeout(function() {
            manager.rejectChanges();
        }, 1000);
        return $q.reject(error); // so downstream promise users know it failed
    }

}

//#region demo operations
function purge(callback) {
    return $http.post(serviceName + '/purge')
    .then(function () {
        logger.success("database purged.");
        manager.clear();
        if (callback) callback();
    })
    .catch(function (error) {
        logger.error("database purge failed: " + error);
        return $q.reject(error); // so downstream promise users know it failed
    });
}

function reset(callback) {
    return $http.post(serviceName + '/reset')
    .then(function () {
        logger.success("database reset.");
        manager.clear();
        if (callback) callback();
    })
    .catch(function (error) {
        logger.error("database reset failed: " + error);
        return $q.reject(error); // so downstream promise users know it failed
    });
}

//#endregion

1 个答案:

答案 0 :(得分:1)

您的功能结构不正确。您在dataservice之前提前终止function hasChanges() {功能。此函数以及在其后声明的所有函数不再在dataservice中定义。

使用显示模块模式,您尝试将service.hasChanges分配给hasChanges功能。但是hasChanges函数未在dataservice中定义,它在dataservice之外定义,因此赋值返回undefined,因此错误Undefined is not a function.

dataservice函数});的结束块向下移动以包含其所有功能。