1

我是新来的微风。我试图在客户端中定义我的实体类型而不从服务器获取元数据。我在服务器实体中有一个名为 ID 的属性。

我使用以下代码将客户端的命名约定默认为驼峰式。

breeze.NamingConvention.camelCase.setAsDefault();

所以,我开始按如下方式映射实体

store.addEntityType({
        shortName: "Photo",
        namespace: "MyProj.Models",
        dataProperties: {
            id: {
                dataType: DataType.Guid,
                isNullable: false,
                isPartOfKey: true
            },
            title: {
                dataType: DataType.String
            },
            description: {
                dataType: DataType.String
            },
            createdDate: {
                dataType: DataType.DateTime
            },
        }
    });

这一切都很好,除了 id 字段没有得到正确的值。相反,它具有由微风数据类型 ctor 设置的默认值,该值等于Guid.Empty.

通过单步执行breathjs 调试脚本,我发现它会Id在来自ajax 请求的数据中查找一个属性名称。但它找不到它,因为属性是ID,所以它将它初始化为empty guid字符串。我假设通过设置nameOnServer的属性dataProperty id,我将能够修复它。

store.addEntityType({
        shortName: "Photo",
        namespace: "MyProj.Models",
        dataProperties: {
            id: {
                dataType: DataType.Guid,
                isNullable: false,
                nameOnServer: 'ID',
                isPartOfKey: true
            },
            title: {
                dataType: DataType.String
            },
            description: {
                dataType: DataType.String
            },
            createdDate: {
                dataType: DataType.DateTime
            },
        }
    });

但它没有用。

进一步挖掘 breez.debug.js 代码,在第updateClientServerNames7154 行的方法中,它似乎忽略了nameOnServer我定义的。

我在这里错过了什么吗?

4

1 回答 1

1

Okay, Feel like I spent my whole life through breeze documentation. Anyways, Finally solved the issue. To be honest, this wasn't a problem in breeze (but I wonder why it doesn't override the actual nameOnServer when I provide one). It's an error made by one of the developers in the early stage of the database implementation (probably 6 years ago). If the database adhered to Pascal Case naming convention, things would have worked perfectly fine.

As a solution I wrote a custom naming convention which corrects the naming convention error when it has ID in the name and combines it with camelCase naming convention.

var createInconsistenIDConvention = function () {
var serverPropertyNameToClient = function (serverPropertyName, prop) {

    if (prop && prop.isDataProperty && (prop.nameOnServer && prop.nameOnServer === "ID")) {
        return "id";
    } else {
        var firstSection = serverPropertyName.substr(0, 1).toLowerCase();
        var idSection = "";
        if (serverPropertyName.substr(1).indexOf("ID") != -1) {
            firstSection += serverPropertyName.substr(1, serverPropertyName.substr(1).indexOf("ID")).toLowerCase() + "Id";
        } else {
            firstSection += serverPropertyName.substr(1);
        }

        return firstSection;
    }
}
var clientPropertyNameToServer = function (clientPropertyName, prop) {
    if (prop && prop.isDataProperty && (prop.nameOnServer && prop.nameOnServer.indexOf("ID") != -1)) {
        return prop.nameOnServer;
    } else {
        return clientPropertyName.substr(0, 1).toUpperCase() + clientPropertyName.substr(1);
    }
}
return new breeze.NamingConvention({
    name: "inconsistenID",
    serverPropertyNameToClient: serverPropertyNameToClient,
    clientPropertyNameToServer: clientPropertyNameToServer
});
};

Not sure if the way I've used nameOnServer property is not correct. I couldn't find any documentation on that in breeze website.

please note that the above code only consider situations like ID, CountryID, GameID, PersonID etc.

Problem solved for now.

于 2013-07-22T15:56:21.127 回答