Merge pull request #199 from fabien/feature/normalize-url
Customize/Normalize class-level remoting http path
This commit is contained in:
commit
c0dce8ff63
|
@ -111,6 +111,9 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
||||||
var pluralName = (settings && settings.plural) ||
|
var pluralName = (settings && settings.plural) ||
|
||||||
inflection.pluralize(className);
|
inflection.pluralize(className);
|
||||||
|
|
||||||
|
var httpOptions = (settings && settings.http) || {};
|
||||||
|
var pathName = httpOptions.path || pluralName;
|
||||||
|
|
||||||
if (!className) {
|
if (!className) {
|
||||||
throw new Error('Class name required');
|
throw new Error('Class name required');
|
||||||
}
|
}
|
||||||
|
@ -188,7 +191,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
||||||
hiddenProperty(ModelClass, 'dataSource', modelBuilder); // Keep for back-compatibility
|
hiddenProperty(ModelClass, 'dataSource', modelBuilder); // Keep for back-compatibility
|
||||||
hiddenProperty(ModelClass, 'pluralModelName', pluralName);
|
hiddenProperty(ModelClass, 'pluralModelName', pluralName);
|
||||||
hiddenProperty(ModelClass, 'relations', {});
|
hiddenProperty(ModelClass, 'relations', {});
|
||||||
hiddenProperty(ModelClass, 'http', { path: '/' + pluralName });
|
hiddenProperty(ModelClass, 'http', { path: '/' + pathName });
|
||||||
hiddenProperty(ModelClass, 'base', ModelBaseClass);
|
hiddenProperty(ModelClass, 'base', ModelBaseClass);
|
||||||
|
|
||||||
// inherit ModelBaseClass static methods
|
// inherit ModelBaseClass static methods
|
||||||
|
|
|
@ -70,11 +70,7 @@ function extendScopeMethods(definition, scopeMethods, ext) {
|
||||||
return relationMethod.apply(relation, arguments);
|
return relationMethod.apply(relation, arguments);
|
||||||
};
|
};
|
||||||
if (relationMethod.shared) {
|
if (relationMethod.shared) {
|
||||||
method.shared = true;
|
sharedMethod(definition, key, method, relationMethod);
|
||||||
method.accepts = relationMethod.accepts;
|
|
||||||
method.returns = relationMethod.returns;
|
|
||||||
method.http = relationMethod.http;
|
|
||||||
method.description = relationMethod.description;
|
|
||||||
}
|
}
|
||||||
customMethods.push(key);
|
customMethods.push(key);
|
||||||
}
|
}
|
||||||
|
@ -573,14 +569,18 @@ function scopeMethod(definition, methodName) {
|
||||||
|
|
||||||
var relationMethod = relationClass.prototype[methodName];
|
var relationMethod = relationClass.prototype[methodName];
|
||||||
if (relationMethod.shared) {
|
if (relationMethod.shared) {
|
||||||
|
sharedMethod(definition, methodName, method, relationMethod);
|
||||||
|
}
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sharedMethod(definition, methodName, method, relationMethod) {
|
||||||
method.shared = true;
|
method.shared = true;
|
||||||
method.accepts = relationMethod.accepts;
|
method.accepts = relationMethod.accepts;
|
||||||
method.returns = relationMethod.returns;
|
method.returns = relationMethod.returns;
|
||||||
method.http = relationMethod.http;
|
method.http = relationMethod.http;
|
||||||
method.description = relationMethod.description;
|
method.description = relationMethod.description;
|
||||||
}
|
};
|
||||||
return method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a related item by foreign key
|
* Find a related item by foreign key
|
||||||
|
@ -1295,6 +1295,15 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
|
||||||
return relationMethod;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -549,6 +549,15 @@ describe('DataSource define model', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should allow an explicit remoting path', function () {
|
||||||
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
|
var User = ds.define('User', {name: String, bio: String}, {
|
||||||
|
http: { path: 'accounts' }
|
||||||
|
});
|
||||||
|
User.http.path.should.equal('/accounts');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Load models with base', function () {
|
describe('Load models with base', function () {
|
||||||
|
|
Loading…
Reference in New Issue