diff --git a/lib/application.js b/lib/application.js index 37eba58b..2cc69e0e 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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; } diff --git a/lib/middleware/rest.js b/lib/middleware/rest.js index e606b54a..a22dadcf 100644 --- a/lib/middleware/rest.js +++ b/lib/middleware/rest.js @@ -3,7 +3,7 @@ */ var asteroid = require('../asteroid'); -var RemoteObjects = require('sl-remoting') +var RemoteObjects = require('sl-remoting'); /** * Export the middleware.