Merge pull request #510 from strongloop/fix/remoting-type-conversion

Fix coercion for remoting on vanilla models
This commit is contained in:
Ritchie Martori 2014-09-03 11:55:04 -07:00
commit 6a604de157
3 changed files with 41 additions and 7 deletions

View File

@ -3,6 +3,7 @@
*/ */
var registry = require('../registry'); var registry = require('../registry');
var assert = require('assert'); var assert = require('assert');
var RemoteObjects = require('strong-remoting');
var SharedClass = require('strong-remoting').SharedClass; var SharedClass = require('strong-remoting').SharedClass;
var extend = require('util')._extend; var extend = require('util')._extend;
var stringUtils = require('underscore.string'); var stringUtils = require('underscore.string');
@ -100,6 +101,7 @@ var Model = module.exports = registry.modelBuilder.define('Model');
Model.setup = function () { Model.setup = function () {
var ModelCtor = this; var ModelCtor = this;
var options = this.settings; var options = this.settings;
var typeName = this.modelName;
// create a sharedClass // create a sharedClass
var sharedClass = ModelCtor.sharedClass = new SharedClass( var sharedClass = ModelCtor.sharedClass = new SharedClass(
@ -108,6 +110,11 @@ Model.setup = function () {
options.remoting options.remoting
); );
// setup a remoting type converter for this model
RemoteObjects.convert(typeName, function(val) {
return val ? new ModelCtor(val) : val;
});
// support remoting prototype methods // support remoting prototype methods
ModelCtor.sharedCtor = function (data, id, fn) { ModelCtor.sharedCtor = function (data, id, fn) {
var ModelCtor = this; var ModelCtor = this;

View File

@ -4,7 +4,6 @@
var Model = require('./model'); var Model = require('./model');
var runtime = require('../runtime'); var runtime = require('../runtime');
var RemoteObjects = require('strong-remoting');
var assert = require('assert'); var assert = require('assert');
var async = require('async'); var async = require('async');
@ -37,12 +36,6 @@ PersistedModel.setup = function setupPersistedModel() {
Model.setup.call(this); Model.setup.call(this);
var PersistedModel = this; var PersistedModel = this;
var typeName = this.modelName;
// setup a remoting type converter for this model
RemoteObjects.convert(typeName, function(val) {
return val ? new PersistedModel(val) : val;
});
// enable change tracking (usually for replication) // enable change tracking (usually for replication)
if(this.settings.trackChanges) { if(this.settings.trackChanges) {

View File

@ -0,0 +1,34 @@
var loopback = require('loopback');
var request = require('supertest');
describe('remoting coercion', function() {
it('should coerce arguments based on the type', function(done) {
var called = false;
var app = loopback();
app.use(loopback.rest());
var TestModel = app.model('TestModel', {base: 'Model', dataSource: null, public: true});
TestModel.test = function(inst, cb) {
called = true;
assert(inst instanceof TestModel);
assert(inst.foo === 'bar');
cb();
}
TestModel.remoteMethod('test', {
accepts: {arg: 'inst', type: 'TestModel', http: {source: 'body'}},
http: {path: '/test', verb: 'post'}
});
request(app)
.post('/TestModels/test')
.set('Content-Type', 'application/json')
.send({
foo: 'bar'
})
.end(function(err) {
if(err) return done(err);
assert(called);
done();
});
});
})