-
Notifications
You must be signed in to change notification settings - Fork 44
/
breeze.namingConventionWithDictionary.js
118 lines (106 loc) · 4.62 KB
/
breeze.namingConventionWithDictionary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//#region Copyright, Version, and Description
/*
*
* NamingConventionWithDictionary plugin to the breeze.NamingConvention class
*
* Adds a NamingConvention that extends another NamingConvention
* by attempting first to translate specific Entity property names using a dictionary.
* If a property name is not found in the dictionary,
* it falls back to the base NamingConvention (AKA "sourceNamingConvention").
*
* Copyright 2015 IdeaBlade, Inc. All Rights Reserved.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* conditions of the IdeaBlade Breeze license, available at http://www.breezejs.com/license
*
* Author: Ward Bell
* Version: 0.1.0 - original
*
* Load this script after breeze
*
* Usage:
* var convention =
* new breeze.NamingConvention.NamingConventionWithDictionary(...)
*
*/
//#endregion
(function (definition) {
if (typeof breeze === "object") {
definition(breeze);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node
var b = require('breeze-client');
definition(b);
} else if (typeof define === "function" && define["amd"]) {
// Requirejs / AMD
define(['breeze-client'], definition);
} else {
throw new Error("Can't find breeze");
}
}(function (breeze) {
'use strict';
breeze.NamingConvention.NamingConventionWithDictionary = NamingConventionWithDictionary;
/**
NamingConvention that extends another {{#crossLink "NamingConvention"}}{{/crossLink}}
by attempting first to translate specific Entity property names using a dictionary.
If a property name is not found in the dictionary,
it falls back to the base NamingConvention (AKA "sourceNamingConvention").
@class NamingConventionWithDictionary
**/
/**
NamingConventionWithDictionary constructor
@example
var clientToServerDictionary = {
'Customer:#Northwind.Models': {customerName: 'CompanyName', zip: 'PostalCode'},
'Order:#Northwind.Models': {freightCost: 'Freight'},
undefined: {foo: 'Bar'} // translation for expected anonymous type property
};
var convention = new breeze.NamingConvention.NamingConventionWithDictionary(
'northwind', breeze.NamingConvention.camelCase, clientToServerDictionary);
convention.setAsDefault(); // make it the default for the entire app
@method <ctor> NamingConventionWithDictionary
@param name {String}
@param sourceNamingConvention {Function} {{#crossLink "NamingConvention"}}{{/crossLink}} base class
@param clientToServerDictionary {Object} Hash of EntityType names with inner hash of client-to-server key/value pairs
*/
function NamingConventionWithDictionary(name, sourceConvention, clientToServerDictionary) {
breeze.core.assertParam(name, 'name').isNonEmptyString().check();
breeze.core.assertParam(sourceConvention, 'sourceConvention').isInstanceOf(breeze.NamingConvention).check();
breeze.core.assertParam(clientToServerDictionary, 'clientToServerDictionary').isObject().check();
var serverToClientDictionary = makeServerToClientDictionary();
return new breeze.NamingConvention({
name: name,
clientPropertyNameToServer: clientPropertyNameToServer,
serverPropertyNameToClient: serverPropertyNameToClient
});
function clientPropertyNameToServer(name, propDef) {
var typeName = propDef && propDef.parentType && propDef.parentType.name;
var props = clientToServerDictionary[typeName || undefined];
var newName = props && props[name];
return newName || sourceConvention.clientPropertyNameToServer(name, propDef);
}
function serverPropertyNameToClient(name, propDef) {
var typeName = propDef && propDef.parentType && propDef.parentType.name;
var props = serverToClientDictionary[typeName || undefined];
var newName = props && props[name];
return newName || sourceConvention.serverPropertyNameToClient(name, propDef);
}
// makes new dictionary based on clientToServerDictionary
// that reverses each EntityType's {clientPropName: serverPropName} KV pairs
function makeServerToClientDictionary() {
var dict = {};
for (var typeName in clientToServerDictionary) {
if (clientToServerDictionary.hasOwnProperty(typeName)) {
var newType = {}
var type = clientToServerDictionary[typeName];
for (var prop in type) {
if (type.hasOwnProperty(prop)) {
newType[type[prop]] = prop; // reverse KV pair
}
}
dict[typeName] = newType;
}
}
return dict;
}
}
}));