Fix sharedCtor

This commit is contained in:
Ritchie 2013-06-05 09:35:35 -07:00
parent 95050f43e3
commit 583f87a72b
2 changed files with 26 additions and 15 deletions

View File

@ -41,12 +41,6 @@ app.disuse = function (route) {
}
}
/**
* App models.
*/
app.models = {};
/**
* Get ModelBuilder.
*/
@ -67,7 +61,6 @@ app.model =
app.defineModel =
app.define = function (name, properties, options) {
var modelBuilder = this.modelBuilder();
var ModelCtor = modelBuilder.define(name, properties, options);
ModelCtor.dataSource = function (name) {
@ -77,6 +70,8 @@ app.define = function (name, properties, options) {
var hasMany = ModelCtor.hasMany;
if(!hasMany) return;
// override the default relations to add shared proxy methods
// cannot expose the relation methods since they are only defined
// once you get them (eg. prototype[name])
@ -107,24 +102,29 @@ app.define = function (name, properties, options) {
var fn = this.prototype[proxyMethodName] = function () {
// this cannot be a shared method
// because it is defined when you
// get it...
// inside a property getter...
this[methodName].apply(thisClass, arguments);
};
fn.shared = true;
fn.http = {verb: 'get', path: '/' + methodName};
hasMany.apply(this, arguments);
};
};
ModelCtor.shared = true;
ModelCtor.sharedCtor = function (id, fn) {
ModelCtor.find(id, fn);
if(id) {
ModelCtor.find(id, fn);
} else {
fn(null, new ModelCtor(data));
}
};
ModelCtor.sharedCtor.accepts = [
// todo... models need to expose what id type they need
{arg: 'id', type: 'number', optional: true},
{arg: 'data', type: 'object', optional: true}
{arg: 'id', type: 'any'},
{arg: 'data', type: 'object'}
];
ModelCtor.sharedCtor.http = [
{path: '/'},
@ -141,6 +141,7 @@ app.define = function (name, properties, options) {
app.models = function () {
var models = this._models;
var result = {};
var dataSources = this.dataSources;
// add in any model from a data source
@ -149,11 +150,21 @@ app.models = function () {
Object.keys(dataSource.models).forEach(function (className) {
var model = dataSource.models[className];
models[i8n.pluralize(model.modelName)] = model;
result[exportedName(model)] = model;
});
});
return models;
// add in defined models
Object.keys(models).forEach(function (name) {
var model = models[name];
result[exportedName(model)] = model;
});
function exportedName(model) {
return model.pluralModelName || i8n.pluralize(model.modelName);
}
return result;
}

View File

@ -3,7 +3,7 @@
*/
var asteroid = require('../asteroid');
var RemoteObjects = require('sl-remoting')
var RemoteObjects = require('sl-remoting');
/**
* Export the middleware.