Fix save implementation for remoting connector
This commit is contained in:
parent
7c0a470d64
commit
940e11125d
|
@ -2,6 +2,7 @@
|
|||
* Module Dependencies.
|
||||
*/
|
||||
var Model = require('./model');
|
||||
var DataAccess = require('loopback-datasource-juggler/lib/dao');
|
||||
|
||||
/**
|
||||
* Extends Model with basic query and CRUD support.
|
||||
|
@ -258,7 +259,21 @@ setRemoting(DataModel.count, {
|
|||
*/
|
||||
|
||||
DataModel.prototype.save = function (options, callback) {
|
||||
throwNotAttached(this.constructor.modelName, 'save');
|
||||
var inst = this;
|
||||
var DataModel = inst.constructor;
|
||||
|
||||
if(typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
// delegates directly to DataAccess
|
||||
DataAccess.prototype.save.call(this, options, function(err, data) {
|
||||
if(err) return callback(data);
|
||||
var saved = new DataModel(data);
|
||||
inst.setId(saved.getId());
|
||||
callback(null, data);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
@ -329,3 +344,46 @@ setRemoting(DataModel.prototype.updateAttributes, {
|
|||
DataModel.prototype.reload = function reload(callback) {
|
||||
throwNotAttached(this.constructor.modelName, 'reload');
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the corret `id` property for the `DataModel`. If a `Connector` defines
|
||||
* a `setId` method it will be used. Otherwise the default lookup is used. You
|
||||
* should override this method to handle complex ids.
|
||||
*
|
||||
* @param {*} val The `id` value. Will be converted to the type the id property
|
||||
* specifies.
|
||||
*/
|
||||
|
||||
DataModel.prototype.setId = function(val) {
|
||||
var ds = this.getDataSource();
|
||||
this[this.getIdName()] = val;
|
||||
}
|
||||
|
||||
DataModel.prototype.getId = function() {
|
||||
var data = this.toObject();
|
||||
if(!data) return;
|
||||
return data[this.getIdName()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the id property name of the constructor.
|
||||
*/
|
||||
|
||||
DataModel.prototype.getIdName = function() {
|
||||
return this.constructor.getIdName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the id property name
|
||||
*/
|
||||
|
||||
DataModel.getIdName = function() {
|
||||
var Model = this;
|
||||
var ds = Model.getDataSource();
|
||||
|
||||
if(ds.idName) {
|
||||
return ds.idName(Model.modelName);
|
||||
} else {
|
||||
return 'id';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,4 +25,15 @@ describe('RemoteConnector', function() {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to call save', function (done) {
|
||||
var m = new TestModel({
|
||||
foo: 'bar'
|
||||
});
|
||||
m.save(function(err, data) {
|
||||
if(err) return done(err);
|
||||
assert(m.id);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -30,4 +30,14 @@ describe('RemoteConnector', function() {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should alow instance methods to be called remotely', function (done) {
|
||||
var data = {foo: 'bar'};
|
||||
var m = new this.LocalModel(data);
|
||||
m.save(function(err, result) {
|
||||
if(err) return done(err);
|
||||
expect(result).to.deep.equal({id: 2, foo: 'bar'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue