Changed normalization api - enabled hasOne remoting

This commit is contained in:
Fabien Franzen 2014-08-01 11:24:41 +02:00
parent 71bf0f8240
commit ef65ffee48
2 changed files with 25 additions and 15 deletions

View File

@ -53,6 +53,10 @@ function isModelClass(cls) {
return cls.prototype instanceof DefaultModelBaseClass;
}
ModelBuilder.normalizePathName = function(pathName, className) {
return inflection.transform(pathName, ['underscore', 'dasherize']);
};
/**
* Get a model by name.
*
@ -112,13 +116,10 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
inflection.pluralize(className);
var pathName = (settings && settings.path) || pluralName;
var normalize = ModelBuilder.normalizePathName === true || (settings && settings.normalize);
var normalize = settings && settings.normalize;
if (this.normalizePathName || (normalize && this.normalizePathName !== false)) {
pathName = inflection.transform(pathName, ['underscore', 'dasherize']);
} else if (typeof ModelBuilder.normalizePathName === 'function' &&
this.normalizePathName !== false) {
pathName = ModelBuilder.normalizePathName.apply(this, [pathName].concat(args));
pathName = ModelBuilder.normalizePathName(pathName, className);
}
if (!className) {

View File

@ -70,11 +70,7 @@ function extendScopeMethods(definition, scopeMethods, ext) {
return relationMethod.apply(relation, arguments);
};
if (relationMethod.shared) {
method.shared = true;
method.accepts = relationMethod.accepts;
method.returns = relationMethod.returns;
method.http = relationMethod.http;
method.description = relationMethod.description;
sharedMethod(definition, key, method, relationMethod);
}
customMethods.push(key);
}
@ -573,15 +569,19 @@ function scopeMethod(definition, methodName) {
var relationMethod = relationClass.prototype[methodName];
if (relationMethod.shared) {
method.shared = true;
method.accepts = relationMethod.accepts;
method.returns = relationMethod.returns;
method.http = relationMethod.http;
method.description = relationMethod.description;
sharedMethod(definition, methodName, method, relationMethod);
}
return method;
}
function sharedMethod(definition, methodName, method, relationMethod) {
method.shared = true;
method.accepts = relationMethod.accepts;
method.returns = relationMethod.returns;
method.http = relationMethod.http;
method.description = relationMethod.description;
};
/**
* Find a related item by foreign key
* @param {*} fkId The foreign key
@ -1295,6 +1295,15 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
return relationMethod;
}
});
// FIXME: [rfeng] Wrap the property into a function for remoting
// so that it can be accessed as /api/<model>/<id>/<hasOneRelationName>
// For example, /api/orders/1/customer
var fn = function() {
var f = this[relationName];
f.apply(this, arguments);
};
modelFrom.prototype['__get__' + relationName] = fn;
};
/**