Merge pull request #658 from rhalff:master
Fix basic jshint errors Close #658
This commit is contained in:
commit
97a8c3acb2
|
@ -5,8 +5,10 @@
|
|||
"indent": 2,
|
||||
"undef": true,
|
||||
"quotmark": "single",
|
||||
"maxlen": 80,
|
||||
"maxlen": 150,
|
||||
"trailing": true,
|
||||
"newcap": true,
|
||||
"nonew": true
|
||||
"nonew": true,
|
||||
"laxcomma": true,
|
||||
"laxbreak": true
|
||||
}
|
||||
|
|
19
Gruntfile.js
19
Gruntfile.js
|
@ -30,8 +30,11 @@ module.exports = function(grunt) {
|
|||
gruntfile: {
|
||||
src: 'Gruntfile.js'
|
||||
},
|
||||
lib_test: {
|
||||
src: ['lib/**/*.js', 'test/**/*.js']
|
||||
lib: {
|
||||
src: ['lib/**/*.js']
|
||||
},
|
||||
test: {
|
||||
src: ['test/**/*.js']
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
|
@ -39,9 +42,13 @@ module.exports = function(grunt) {
|
|||
files: '<%= jshint.gruntfile.src %>',
|
||||
tasks: ['jshint:gruntfile']
|
||||
},
|
||||
lib_test: {
|
||||
files: '<%= jshint.lib_test.src %>',
|
||||
tasks: ['jshint:lib_test']
|
||||
lib: {
|
||||
files: ['<%= jshint.lib.src %>'],
|
||||
tasks: ['jshint:lib']
|
||||
},
|
||||
test: {
|
||||
files: ['<%= jshint.test.src %>'],
|
||||
tasks: ['jshint:test']
|
||||
}
|
||||
},
|
||||
browserify: {
|
||||
|
@ -104,7 +111,7 @@ module.exports = function(grunt) {
|
|||
|
||||
// list of files to exclude
|
||||
exclude: [
|
||||
|
||||
|
||||
],
|
||||
|
||||
// test results reporter to use
|
||||
|
|
|
@ -39,15 +39,15 @@ function AccessContext(context) {
|
|||
this.sharedMethod = context.sharedMethod;
|
||||
this.sharedClass = this.sharedMethod && this.sharedMethod.sharedClass;
|
||||
if(this.sharedMethod) {
|
||||
this.methodNames = this.sharedMethod.aliases.concat([this.sharedMethod.name]);
|
||||
this.methodNames = this.sharedMethod.aliases.concat([this.sharedMethod.name]);
|
||||
} else {
|
||||
this.methodNames = [];
|
||||
}
|
||||
|
||||
|
||||
if(this.sharedMethod) {
|
||||
this.accessType = this.model._getAccessTypeForMethod(this.sharedMethod);
|
||||
this.accessType = this.model._getAccessTypeForMethod(this.sharedMethod);
|
||||
}
|
||||
|
||||
|
||||
this.accessType = context.accessType || AccessContext.ALL;
|
||||
assert(loopback.AccessToken,
|
||||
'AccessToken model must be defined before AccessContext model');
|
||||
|
@ -157,9 +157,9 @@ AccessContext.prototype.debug = function() {
|
|||
if(debug.enabled) {
|
||||
debug('---AccessContext---');
|
||||
if(this.principals && this.principals.length) {
|
||||
debug('principals:')
|
||||
debug('principals:');
|
||||
this.principals.forEach(function(principal) {
|
||||
debug('principal: %j', principal)
|
||||
debug('principal: %j', principal);
|
||||
});
|
||||
} else {
|
||||
debug('principals: %j', this.principals);
|
||||
|
@ -170,14 +170,14 @@ AccessContext.prototype.debug = function() {
|
|||
debug('method %s', this.method);
|
||||
debug('accessType %s', this.accessType);
|
||||
if(this.accessToken) {
|
||||
debug('accessToken:')
|
||||
debug('accessToken:');
|
||||
debug(' id %j', this.accessToken.id);
|
||||
debug(' ttl %j', this.accessToken.ttl);
|
||||
}
|
||||
debug('getUserId() %s', this.getUserId());
|
||||
debug('isAuthenticated() %s', this.isAuthenticated());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This class represents the abstract notion of a principal, which can be used
|
||||
|
@ -273,17 +273,17 @@ AccessRequest.prototype.exactlyMatches = function(acl) {
|
|||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Is the request for access allowed?
|
||||
*
|
||||
*
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
AccessRequest.prototype.isAllowed = function() {
|
||||
return this.permission !== loopback.ACL.DENY;
|
||||
}
|
||||
};
|
||||
|
||||
AccessRequest.prototype.debug = function() {
|
||||
if(debug.enabled) {
|
||||
|
@ -295,7 +295,7 @@ AccessRequest.prototype.debug = function() {
|
|||
debug(' isWildcard() %s', this.isWildcard());
|
||||
debug(' isAllowed() %s', this.isAllowed());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.AccessContext = AccessContext;
|
||||
module.exports.Principal = Principal;
|
||||
|
|
|
@ -14,22 +14,22 @@ var DataSource = require('loopback-datasource-juggler').DataSource
|
|||
|
||||
/**
|
||||
* The `App` object represents a Loopback application.
|
||||
*
|
||||
*
|
||||
* The App object extends [Express](http://expressjs.com/api.html#express) and
|
||||
* supports Express middleware. See
|
||||
* [Express documentation](http://expressjs.com/) for details.
|
||||
*
|
||||
*
|
||||
* ```js
|
||||
* var loopback = require('loopback');
|
||||
* var app = loopback();
|
||||
*
|
||||
*
|
||||
* app.get('/', function(req, res){
|
||||
* res.send('hello world');
|
||||
* });
|
||||
*
|
||||
*
|
||||
* app.listen(3000);
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* @class LoopBackApplication
|
||||
* @header var app = loopback()
|
||||
*/
|
||||
|
@ -59,10 +59,10 @@ app.remotes = function () {
|
|||
if(this.get) {
|
||||
options = this.get('remoting');
|
||||
}
|
||||
|
||||
|
||||
return (this._remotes = RemoteObjects.create(options));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Remove a route by reference.
|
||||
|
@ -76,7 +76,7 @@ app.disuse = function (route) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Attach a model to the app. The `Model` will be available on the
|
||||
|
@ -92,7 +92,7 @@ app.disuse = function (route) {
|
|||
* var User = loopback.User;
|
||||
* app.model(User, { dataSource: 'db' });
|
||||
*```
|
||||
*
|
||||
*
|
||||
* @param {Object|String} Model The model to attach.
|
||||
* @options {Object} config The model's configuration.
|
||||
* @property {String|DataSource} dataSource The `DataSource` to which to attach the model.
|
||||
|
@ -207,7 +207,7 @@ app.model = function (Model, config) {
|
|||
|
||||
app.models = function () {
|
||||
return this._models || (this._models = []);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Define a DataSource.
|
||||
|
@ -221,7 +221,7 @@ app.dataSource = function (name, config) {
|
|||
this.dataSources[classify(name)] =
|
||||
this.dataSources[camelize(name)] = ds;
|
||||
return ds;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Register a connector.
|
||||
|
@ -254,30 +254,30 @@ app.remoteObjects = function () {
|
|||
this.remotes().classes().forEach(function(sharedClass) {
|
||||
result[sharedClass.name] = sharedClass.ctor;
|
||||
});
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Get a handler of the specified type from the handler cache.
|
||||
* @triggers `mounted` events on shared class constructors (models)
|
||||
* @triggers `mounted` events on shared class constructors (models)
|
||||
*/
|
||||
|
||||
|
||||
app.handler = function (type, options) {
|
||||
var handlers = this._handlers || (this._handlers = {});
|
||||
if(handlers[type]) {
|
||||
return handlers[type];
|
||||
}
|
||||
|
||||
|
||||
var remotes = this.remotes();
|
||||
var handler = this._handlers[type] = remotes.handler(type, options);
|
||||
|
||||
|
||||
remotes.classes().forEach(function(sharedClass) {
|
||||
sharedClass.ctor.emit('mounted', app, sharedClass, remotes);
|
||||
});
|
||||
|
||||
|
||||
return handler;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* An object to store dataSource instances.
|
||||
|
@ -342,7 +342,7 @@ app.enableAuth = function() {
|
|||
app.boot = function(options) {
|
||||
throw new Error(
|
||||
'`app.boot` was removed, use the new module loopback-boot instead');
|
||||
}
|
||||
};
|
||||
|
||||
function classify(str) {
|
||||
return stringUtils.classify(str);
|
||||
|
@ -355,7 +355,7 @@ function camelize(str) {
|
|||
function dataSourcesFromConfig(config, connectorRegistry) {
|
||||
var connectorPath;
|
||||
|
||||
assert(typeof config === 'object',
|
||||
assert(typeof config === 'object',
|
||||
'cannont create data source without config object');
|
||||
|
||||
if(typeof config.connector === 'string') {
|
||||
|
@ -459,7 +459,7 @@ app.listen = function(cb) {
|
|||
});
|
||||
|
||||
var useAppConfig =
|
||||
arguments.length == 0 ||
|
||||
arguments.length === 0 ||
|
||||
(arguments.length == 1 && typeof arguments[0] == 'function');
|
||||
|
||||
if (useAppConfig) {
|
||||
|
@ -469,4 +469,4 @@ app.listen = function(cb) {
|
|||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -7,13 +7,13 @@ module.exports = Connector;
|
|||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
, debug = require('debug')('connector')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert');
|
||||
|
||||
|
||||
/**
|
||||
* Create a new `Connector` with the given `options`.
|
||||
*
|
||||
|
@ -24,7 +24,7 @@ var EventEmitter = require('events').EventEmitter
|
|||
function Connector(options) {
|
||||
EventEmitter.apply(this, arguments);
|
||||
this.options = options;
|
||||
|
||||
|
||||
debug('created with options', options);
|
||||
}
|
||||
|
||||
|
@ -43,12 +43,12 @@ Connector._createJDBAdapter = function (jdbModule) {
|
|||
jdbModule.initialize(fauxSchema, function () {
|
||||
// connected
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Add default crud operations from a JugglingDB adapter.
|
||||
*/
|
||||
|
||||
Connector.prototype._addCrudOperationsFromJDBAdapter = function (connector) {
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
var mailer = require('nodemailer')
|
||||
, assert = require('assert')
|
||||
, debug = require('debug')('loopback:connector:mail')
|
||||
, loopback = require('../loopback')
|
||||
, loopback = require('../loopback');
|
||||
|
||||
/**
|
||||
* Export the MailConnector class.
|
||||
|
@ -44,7 +44,7 @@ function MailConnector(settings) {
|
|||
MailConnector.initialize = function(dataSource, callback) {
|
||||
dataSource.connector = new MailConnector(dataSource.settings);
|
||||
callback();
|
||||
}
|
||||
};
|
||||
|
||||
MailConnector.prototype.DataAccessObject = Mailer;
|
||||
|
||||
|
@ -86,7 +86,7 @@ MailConnector.prototype.setupTransport = function(setting) {
|
|||
|
||||
connector.transportsIndex[setting.type] = transport;
|
||||
connector.transports.push(transport);
|
||||
}
|
||||
};
|
||||
|
||||
function Mailer() {
|
||||
|
||||
|
@ -101,7 +101,7 @@ function Mailer() {
|
|||
|
||||
MailConnector.prototype.transportForName = function(name) {
|
||||
return this.transportsIndex[name];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the default transport.
|
||||
|
@ -111,7 +111,7 @@ MailConnector.prototype.transportForName = function(name) {
|
|||
|
||||
MailConnector.prototype.defaultTransport = function() {
|
||||
return this.transports[0] || this.stubTransport;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Send an email with the given `options`.
|
||||
|
@ -166,7 +166,7 @@ Mailer.send = function (options, fn) {
|
|||
fn(null, options);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Send an email instance using `modelInstance.send()`.
|
||||
|
@ -174,7 +174,7 @@ Mailer.send = function (options, fn) {
|
|||
|
||||
Mailer.prototype.send = function (fn) {
|
||||
this.constructor.send(this, fn);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Access the node mailer object.
|
||||
|
|
|
@ -7,14 +7,14 @@ module.exports = Memory;
|
|||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
|
||||
var Connector = require('./base-connector')
|
||||
, debug = require('debug')('memory')
|
||||
, util = require('util')
|
||||
, inherits = util.inherits
|
||||
, assert = require('assert')
|
||||
, JdbMemory = require('loopback-datasource-juggler/lib/connectors/memory');
|
||||
|
||||
|
||||
/**
|
||||
* Create a new `Memory` connector with the given `options`.
|
||||
*
|
||||
|
|
|
@ -20,20 +20,20 @@ function createMiddlewareNotInstalled(memberName, moduleName) {
|
|||
}
|
||||
|
||||
var middlewareModules = {
|
||||
"compress": "compression",
|
||||
"timeout": "connect-timeout",
|
||||
"cookieParser": "cookie-parser",
|
||||
"cookieSession": "cookie-session",
|
||||
"csrf": "csurf",
|
||||
"errorHandler": "errorhandler",
|
||||
"session": "express-session",
|
||||
"methodOverride": "method-override",
|
||||
"logger": "morgan",
|
||||
"responseTime": "response-time",
|
||||
"favicon": "serve-favicon",
|
||||
"directory": "serve-index",
|
||||
// "static": "serve-static",
|
||||
"vhost": "vhost"
|
||||
'compress': 'compression',
|
||||
'timeout': 'connect-timeout',
|
||||
'cookieParser': 'cookie-parser',
|
||||
'cookieSession': 'cookie-session',
|
||||
'csrf': 'csurf',
|
||||
'errorHandler': 'errorhandler',
|
||||
'session': 'express-session',
|
||||
'methodOverride': 'method-override',
|
||||
'logger': 'morgan',
|
||||
'responseTime': 'response-time',
|
||||
'favicon': 'serve-favicon',
|
||||
'directory': 'serve-index',
|
||||
// 'static': 'serve-static',
|
||||
'vhost': 'vhost'
|
||||
};
|
||||
|
||||
middlewares.bodyParser = safeRequire('body-parser');
|
||||
|
|
|
@ -14,7 +14,7 @@ module.exports = status;
|
|||
* "uptime": 9.394
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* @header loopback.status()
|
||||
*/
|
||||
function status() {
|
||||
|
@ -25,6 +25,6 @@ function status() {
|
|||
started: started,
|
||||
uptime: (Date.now() - Number(started)) / 1000
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -11,22 +11,22 @@ var assert = require('assert');
|
|||
|
||||
module.exports = token;
|
||||
|
||||
/**
|
||||
/**
|
||||
* Check for an access token in cookies, headers, and query string parameters.
|
||||
* This function always checks for the following:
|
||||
*
|
||||
*
|
||||
* - `access_token` (params only)
|
||||
* - `X-Access-Token` (headers only)
|
||||
* - `authorization` (headers and cookies)
|
||||
*
|
||||
* It checks for these values in cookies, headers, and query string parameters _in addition_ to the items
|
||||
* specified in the options parameter.
|
||||
*
|
||||
*
|
||||
* **NOTE:** This function only checks for [signed cookies](http://expressjs.com/api.html#req.signedCookies).
|
||||
*
|
||||
*
|
||||
* The following example illustrates how to check for an `accessToken` in a custom cookie, query string parameter
|
||||
* and header called `foo-auth`.
|
||||
*
|
||||
*
|
||||
* ```js
|
||||
* app.use(loopback.token({
|
||||
* cookies: ['foo-auth'],
|
||||
|
@ -47,13 +47,13 @@ function token(options) {
|
|||
options = options || {};
|
||||
var TokenModel = options.model || loopback.AccessToken;
|
||||
assert(TokenModel, 'loopback.token() middleware requires a AccessToken model');
|
||||
|
||||
|
||||
return function (req, res, next) {
|
||||
if (req.accessToken !== undefined) return next();
|
||||
TokenModel.findForRequest(req, options, function(err, token) {
|
||||
req.accessToken = token || null;
|
||||
next(err);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -15,5 +15,5 @@ function urlNotFound() {
|
|||
var error = new Error('Cannot ' + req.method + ' ' + req.url);
|
||||
error.status = 404;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
106
lib/model.js
106
lib/model.js
|
@ -9,7 +9,7 @@ var extend = require('util')._extend;
|
|||
var stringUtils = require('underscore.string');
|
||||
|
||||
/**
|
||||
* The base class for **all models**.
|
||||
* The base class for **all models**.
|
||||
*
|
||||
* **Inheriting from `Model`**
|
||||
*
|
||||
|
@ -18,7 +18,7 @@ var stringUtils = require('underscore.string');
|
|||
* var options = {...};
|
||||
* var MyModel = loopback.Model.extend('MyModel', properties, options);
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* **Options**
|
||||
*
|
||||
* - `trackChanges` - If true, changes to the model will be tracked. **Required
|
||||
|
@ -27,7 +27,7 @@ var stringUtils = require('underscore.string');
|
|||
* **Events**
|
||||
*
|
||||
* #### Event: `changed`
|
||||
*
|
||||
*
|
||||
* Emitted after a model has been successfully created, saved, or updated.
|
||||
* Argument: `inst`, model instance, object
|
||||
*
|
||||
|
@ -37,10 +37,10 @@ var stringUtils = require('underscore.string');
|
|||
* // => model with id 1 has been changed
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* #### Event: `deleted`
|
||||
*
|
||||
* Emitted after an individual model has been deleted.
|
||||
*
|
||||
* Emitted after an individual model has been deleted.
|
||||
* Argument: `id`, model ID (number).
|
||||
*
|
||||
* ```js
|
||||
|
@ -51,7 +51,7 @@ var stringUtils = require('underscore.string');
|
|||
* ```
|
||||
*
|
||||
* #### Event: `deletedAll`
|
||||
*
|
||||
*
|
||||
* Emitted after an individual model has been deleted.
|
||||
* Argument: `where` (optional), where filter, JSON object.
|
||||
*
|
||||
|
@ -65,27 +65,27 @@ var stringUtils = require('underscore.string');
|
|||
* }
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* #### Event: `attached`
|
||||
*
|
||||
*
|
||||
* Emitted after a `Model` has been attached to an `app`.
|
||||
*
|
||||
*
|
||||
* #### Event: `dataSourceAttached`
|
||||
*
|
||||
*
|
||||
* Emitted after a `Model` has been attached to a `DataSource`.
|
||||
*
|
||||
*
|
||||
* #### Event: set
|
||||
*
|
||||
*
|
||||
* Emitted when model property is set.
|
||||
* Argument: `inst`, model instance, object
|
||||
*
|
||||
*
|
||||
* ```js
|
||||
* MyModel.on('set', function(inst) {
|
||||
* console.log('model with id %s has been changed', inst.id);
|
||||
* // => model with id 1 has been changed
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* @param {Object} data
|
||||
* @property {String} modelName The name of the model. Static property.
|
||||
* @property {DataSource} dataSource Data source to which the model is connected, if any. Static property.
|
||||
|
@ -128,7 +128,7 @@ Model.setup = function () {
|
|||
id = null;
|
||||
} else if (typeof id === 'function') {
|
||||
fn = id;
|
||||
|
||||
|
||||
if(typeof data !== 'object') {
|
||||
id = data;
|
||||
data = null;
|
||||
|
@ -136,7 +136,7 @@ Model.setup = function () {
|
|||
id = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(id && data) {
|
||||
var model = new ModelCtor(data);
|
||||
model.id = id;
|
||||
|
@ -152,14 +152,14 @@ Model.setup = function () {
|
|||
} else {
|
||||
err = new Error('could not find a model with id ' + id);
|
||||
err.statusCode = 404;
|
||||
|
||||
|
||||
fn(err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
fn(new Error('must specify an id or data'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var idDesc = ModelCtor.modelName + ' id';
|
||||
ModelCtor.sharedCtor.accepts = [
|
||||
|
@ -171,7 +171,7 @@ Model.setup = function () {
|
|||
ModelCtor.sharedCtor.http = [
|
||||
{path: '/:id'}
|
||||
];
|
||||
|
||||
|
||||
ModelCtor.sharedCtor.returns = {root: true};
|
||||
|
||||
// before remote hook
|
||||
|
@ -190,7 +190,7 @@ Model.setup = function () {
|
|||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// after remote hook
|
||||
ModelCtor.afterRemote = function (name, fn) {
|
||||
var self = this;
|
||||
|
@ -280,7 +280,7 @@ Model.checkAccess = function(token, modelId, sharedMethod, ctx, callback) {
|
|||
callback = ctx;
|
||||
ctx = {};
|
||||
}
|
||||
|
||||
|
||||
aclModel.checkAccessForContext({
|
||||
accessToken: token,
|
||||
model: this,
|
||||
|
@ -308,7 +308,7 @@ Model._getAccessTypeForMethod = function(method) {
|
|||
method = {name: method};
|
||||
}
|
||||
assert(
|
||||
typeof method === 'object',
|
||||
typeof method === 'object',
|
||||
'method is a required argument and must be a RemoteMethod object'
|
||||
);
|
||||
|
||||
|
@ -340,7 +340,7 @@ Model._getAccessTypeForMethod = function(method) {
|
|||
default:
|
||||
return ACL.EXECUTE;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the `Application` the Model is attached to.
|
||||
|
@ -361,7 +361,7 @@ Model.getApp = function(callback) {
|
|||
callback(null, Model.app);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Enable remote invocation for the method with the given name.
|
||||
|
@ -382,7 +382,7 @@ Model.remoteMethod = function(name, options) {
|
|||
options.isStatic = true;
|
||||
}
|
||||
this.sharedClass.defineMethod(name, options);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Disable remote invocation for the method with the given name.
|
||||
|
@ -395,7 +395,7 @@ Model.remoteMethod = function(name, options) {
|
|||
|
||||
Model.disableRemoteMethod = function(name, isStatic) {
|
||||
this.sharedClass.disableMethod(name, isStatic || false);
|
||||
}
|
||||
};
|
||||
|
||||
Model.belongsToRemoting = function(relationName, relation, define) {
|
||||
var modelName = relation.modelTo && relation.modelTo.modelName;
|
||||
|
@ -409,7 +409,7 @@ Model.belongsToRemoting = function(relationName, relation, define) {
|
|||
description: 'Fetches belongsTo relation ' + relationName,
|
||||
returns: {arg: relationName, type: modelName, root: true}
|
||||
}, fn);
|
||||
}
|
||||
};
|
||||
|
||||
Model.hasOneRemoting = function(relationName, relation, define) {
|
||||
var fn = this.prototype[relationName];
|
||||
|
@ -421,22 +421,22 @@ Model.hasOneRemoting = function(relationName, relation, define) {
|
|||
description: 'Fetches hasOne relation ' + relationName,
|
||||
returns: {arg: relationName, type: relation.modelTo.modelName, root: true}
|
||||
}, fn);
|
||||
}
|
||||
};
|
||||
|
||||
Model.hasManyRemoting = function (relationName, relation, define) {
|
||||
var pathName = (relation.options.http && relation.options.http.path) || relationName;
|
||||
var toModelName = relation.modelTo.modelName;
|
||||
|
||||
|
||||
function convertNullToNotFoundError(ctx, cb) {
|
||||
if (ctx.result !== null) return cb();
|
||||
|
||||
|
||||
var fk = ctx.getArgByName('fk');
|
||||
var msg = 'Unknown "' + toModelName + '" id "' + fk + '".';
|
||||
var error = new Error(msg);
|
||||
error.statusCode = error.status = 404;
|
||||
cb(error);
|
||||
}
|
||||
|
||||
|
||||
var findByIdFunc = this.prototype['__findById__' + relationName];
|
||||
define('__findById__' + relationName, {
|
||||
isStatic: false,
|
||||
|
@ -568,9 +568,9 @@ Model.scopeRemoting = function(scopeName, scope, define) {
|
|||
http: {verb: 'get', path: '/' + pathName + '/count'},
|
||||
accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'},
|
||||
description: 'Counts ' + scopeName + ' of ' + this.modelName + '.',
|
||||
returns: {arg: 'count', type: 'number'}
|
||||
returns: {arg: 'count', type: 'number'}
|
||||
});
|
||||
|
||||
|
||||
};
|
||||
|
||||
Model.nestRemoting = function(relationName, options, cb) {
|
||||
|
@ -579,7 +579,7 @@ Model.nestRemoting = function(relationName, options, cb) {
|
|||
options = {};
|
||||
}
|
||||
options = options || {};
|
||||
|
||||
|
||||
var regExp = /^__([^_]+)__([^_]+)$/;
|
||||
var relation = this.relations[relationName];
|
||||
if (relation && relation.modelTo && relation.modelTo.sharedClass) {
|
||||
|
@ -587,17 +587,17 @@ Model.nestRemoting = function(relationName, options, cb) {
|
|||
var sharedClass = this.sharedClass;
|
||||
var sharedToClass = relation.modelTo.sharedClass;
|
||||
var toModelName = relation.modelTo.modelName;
|
||||
|
||||
|
||||
var pathName = options.pathName || relation.options.path || relationName;
|
||||
var paramName = options.paramName || 'nk';
|
||||
|
||||
|
||||
var http = [].concat(sharedToClass.http || [])[0];
|
||||
|
||||
|
||||
if (relation.multiple) {
|
||||
var httpPath = pathName + '/:' + paramName;
|
||||
var acceptArgs = [
|
||||
{
|
||||
arg: paramName, type: 'any', http: { source: 'path' },
|
||||
{
|
||||
arg: paramName, type: 'any', http: { source: 'path' },
|
||||
description: 'Foreign key for ' + relation.name,
|
||||
required: true
|
||||
}
|
||||
|
@ -606,7 +606,7 @@ Model.nestRemoting = function(relationName, options, cb) {
|
|||
var httpPath = pathName;
|
||||
var acceptArgs = [];
|
||||
}
|
||||
|
||||
|
||||
// A method should return the method name to use, if it is to be
|
||||
// included as a nested method - a falsy return value will skip.
|
||||
var filter = cb || options.filterMethod || function(method, relation) {
|
||||
|
@ -615,31 +615,31 @@ Model.nestRemoting = function(relationName, options, cb) {
|
|||
return '__' + matches[1] + '__' + relation.name + '__' + matches[2];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
sharedToClass.methods().forEach(function(method) {
|
||||
var methodName;
|
||||
if (!method.isStatic && (methodName = filter(method, relation))) {
|
||||
var prefix = relation.multiple ? '__findById__' : '__get__';
|
||||
var getterName = options.getterName || (prefix + relationName);
|
||||
|
||||
|
||||
var getterFn = relation.modelFrom.prototype[getterName];
|
||||
if (typeof getterFn !== 'function') {
|
||||
throw new Error('Invalid remote method: `' + getterName + '`');
|
||||
}
|
||||
|
||||
|
||||
var nestedFn = relation.modelTo.prototype[method.name];
|
||||
if (typeof nestedFn !== 'function') {
|
||||
throw new Error('Invalid remote method: `' + method.name + '`');
|
||||
}
|
||||
|
||||
|
||||
var opts = {};
|
||||
|
||||
|
||||
opts.accepts = acceptArgs.concat(method.accepts || []);
|
||||
opts.returns = [].concat(method.returns || []);
|
||||
opts.description = method.description;
|
||||
opts.rest = extend({}, method.rest || {});
|
||||
opts.rest.delegateTo = method.name;
|
||||
|
||||
|
||||
opts.http = [];
|
||||
var routes = [].concat(method.http || []);
|
||||
routes.forEach(function(route) {
|
||||
|
@ -649,7 +649,7 @@ Model.nestRemoting = function(relationName, options, cb) {
|
|||
opts.http.push(copy);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (relation.multiple) {
|
||||
sharedClass.defineMethod(methodName, opts, function(fkId) {
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
|
@ -681,17 +681,17 @@ Model.nestRemoting = function(relationName, options, cb) {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (options.hooks === false) return; // don't inherit before/after hooks
|
||||
|
||||
|
||||
self.once('mounted', function(app, sc, remotes) {
|
||||
var listenerTree = extend({}, remotes.listenerTree || {});
|
||||
listenerTree.before = listenerTree.before || {};
|
||||
listenerTree.after = listenerTree.after || {};
|
||||
|
||||
|
||||
var beforeListeners = remotes.listenerTree.before[toModelName] || {};
|
||||
var afterListeners = remotes.listenerTree.after[toModelName] || {};
|
||||
|
||||
|
||||
sharedClass.methods().forEach(function(method) {
|
||||
var delegateTo = method.rest && method.rest.delegateTo;
|
||||
if (delegateTo) {
|
||||
|
@ -711,7 +711,7 @@ Model.nestRemoting = function(relationName, options, cb) {
|
|||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
throw new Error('Relation `' + relationName + '` does not exist for model `' + this.modelName + '`');
|
||||
}
|
||||
|
|
|
@ -43,9 +43,9 @@ PersistedModel.setup = function setupPersistedModel() {
|
|||
PersistedModel.enableChangeTracking();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
PersistedModel.setupRemoting();
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Throw an error telling the user that the method is not available and why.
|
||||
|
@ -260,7 +260,7 @@ PersistedModel.destroyById = function deleteById(id, cb) {
|
|||
/**
|
||||
* Alias for destroyById.
|
||||
*/
|
||||
PersistedModel.removeById = PersistedModel.destroyById
|
||||
PersistedModel.removeById = PersistedModel.destroyById;
|
||||
|
||||
/**
|
||||
* Alias for destroyById.
|
||||
|
@ -338,7 +338,7 @@ PersistedModel.prototype.save = function (options, callback) {
|
|||
// then save
|
||||
function save() {
|
||||
inst.trigger('save', function (saveDone) {
|
||||
inst.trigger('update', function (updateDone) {
|
||||
inst.trigger('update', function (updateDone) {
|
||||
Model.upsert(inst, function(err) {
|
||||
inst._initProperties(data);
|
||||
updateDone.call(inst, function () {
|
||||
|
@ -430,7 +430,7 @@ PersistedModel.prototype.reload = function reload(callback) {
|
|||
PersistedModel.prototype.setId = function(val) {
|
||||
var ds = this.getDataSource();
|
||||
this[this.getIdName()] = val;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the `id` value for the `PersistedModel`.
|
||||
|
@ -442,7 +442,7 @@ PersistedModel.prototype.getId = function() {
|
|||
var data = this.toObject();
|
||||
if(!data) return;
|
||||
return data[this.getIdName()];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the id property name of the constructor.
|
||||
|
@ -452,7 +452,7 @@ PersistedModel.prototype.getId = function() {
|
|||
|
||||
PersistedModel.prototype.getIdName = function() {
|
||||
return this.constructor.getIdName();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the id property name of the constructor.
|
||||
|
@ -469,7 +469,7 @@ PersistedModel.getIdName = function() {
|
|||
} else {
|
||||
return 'id';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
PersistedModel.setupRemoting = function() {
|
||||
var PersistedModel = this;
|
||||
|
@ -597,7 +597,7 @@ PersistedModel.setupRemoting = function() {
|
|||
{arg: 'since', type: 'number', description: 'Find deltas since this checkpoint'},
|
||||
{arg: 'remoteChanges', type: 'array', description: 'an array of change objects',
|
||||
http: {source: 'body'}}
|
||||
],
|
||||
],
|
||||
returns: {arg: 'result', type: 'object', root: true},
|
||||
http: {verb: 'post', path: '/diff'}
|
||||
});
|
||||
|
@ -612,7 +612,7 @@ PersistedModel.setupRemoting = function() {
|
|||
returns: {arg: 'changes', type: 'array', root: true},
|
||||
http: {verb: 'get', path: '/changes'}
|
||||
});
|
||||
|
||||
|
||||
setRemoting(PersistedModel, 'checkpoint', {
|
||||
description: 'Create a checkpoint.',
|
||||
returns: {arg: 'checkpoint', type: 'object', root: true},
|
||||
|
@ -649,22 +649,22 @@ PersistedModel.setupRemoting = function() {
|
|||
http: {verb: 'post', path: '/:id/rectify-change'}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a set of deltas and conflicts since the given checkpoint.
|
||||
*
|
||||
* See `Change.diff()` for details.
|
||||
*
|
||||
*
|
||||
* @param {Number} since Find deltas since this checkpoint
|
||||
* @param {Array} remoteChanges An array of change objects
|
||||
* @param {Function} callback
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
PersistedModel.diff = function(since, remoteChanges, callback) {
|
||||
var Change = this.getChangeModel();
|
||||
Change.diff(this.modelName, since, remoteChanges, callback);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the changes to a model since a given checkpoint. Provide a filter object
|
||||
|
@ -720,11 +720,11 @@ PersistedModel.changes = function(since, filter, callback) {
|
|||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a checkpoint.
|
||||
*
|
||||
*
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
|
@ -736,11 +736,11 @@ PersistedModel.checkpoint = function(cb) {
|
|||
sourceId: sourceId
|
||||
}, cb);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the current checkpoint id.
|
||||
*
|
||||
*
|
||||
* @callback {Function} callback
|
||||
* @param {Error} err
|
||||
* @param {Number} currentCheckpointId
|
||||
|
@ -750,7 +750,7 @@ PersistedModel.checkpoint = function(cb) {
|
|||
PersistedModel.currentCheckpoint = function(cb) {
|
||||
var Checkpoint = this.getChangeModel().getCheckpointModel();
|
||||
Checkpoint.current(cb);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Replicate changes since the given checkpoint to the given target model.
|
||||
|
@ -793,7 +793,7 @@ PersistedModel.replicate = function(since, targetModel, options, callback) {
|
|||
|
||||
callback = callback || function defaultReplicationCallback(err) {
|
||||
if(err) throw err;
|
||||
}
|
||||
};
|
||||
|
||||
var tasks = [
|
||||
getSourceChanges,
|
||||
|
@ -848,13 +848,13 @@ PersistedModel.replicate = function(since, targetModel, options, callback) {
|
|||
|
||||
callback && callback(null, conflicts);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an update list (for `Model.bulkUpdate()`) from a delta list
|
||||
* (result of `Change.diff()`).
|
||||
*
|
||||
* @param {Array} deltas
|
||||
*
|
||||
* @param {Array} deltas
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
|
@ -899,13 +899,13 @@ PersistedModel.createUpdates = function(deltas, cb) {
|
|||
if(err) return cb(err);
|
||||
cb(null, updates);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Apply an update list.
|
||||
*
|
||||
* **Note: this is not atomic**
|
||||
*
|
||||
*
|
||||
* @param {Array} updates An updates list (usually from Model.createUpdates())
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
@ -937,11 +937,11 @@ PersistedModel.bulkUpdate = function(updates, callback) {
|
|||
});
|
||||
|
||||
async.parallel(tasks, callback);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the `Change` model.
|
||||
*
|
||||
*
|
||||
* @throws {Error} Throws an error if the change model is not correctly setup.
|
||||
* @return {Change}
|
||||
*/
|
||||
|
@ -953,11 +953,11 @@ PersistedModel.getChangeModel = function() {
|
|||
assert(isSetup, 'Cannot get a setup Change model');
|
||||
|
||||
return changeModel;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the source identifier for this model / dataSource.
|
||||
*
|
||||
*
|
||||
* @callback {Function} callback
|
||||
* @param {Error} err
|
||||
* @param {String} sourceId
|
||||
|
@ -969,12 +969,12 @@ PersistedModel.getSourceId = function(cb) {
|
|||
this.once('dataSourceAttached', this.getSourceId.bind(this, cb));
|
||||
}
|
||||
assert(
|
||||
dataSource.connector.name,
|
||||
dataSource.connector.name,
|
||||
'Model.getSourceId: cannot get id without dataSource.connector.name'
|
||||
);
|
||||
var id = [dataSource.connector.name, this.modelName].join('-');
|
||||
cb(null, id);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Enable the tracking of changes made to the model. Usually for replication.
|
||||
|
@ -993,11 +993,11 @@ PersistedModel.enableChangeTracking = function() {
|
|||
|
||||
Model.afterSave = function afterSave(next) {
|
||||
Model.rectifyChange(this.getId(), next);
|
||||
}
|
||||
};
|
||||
|
||||
Model.afterDestroy = function afterDestroy(next) {
|
||||
Model.rectifyChange(this.getId(), next);
|
||||
}
|
||||
};
|
||||
|
||||
Model.on('deletedAll', cleanup);
|
||||
|
||||
|
@ -1017,7 +1017,7 @@ PersistedModel.enableChangeTracking = function() {
|
|||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
PersistedModel._defineChangeModel = function() {
|
||||
var BaseChangeModel = registry.getModel('Change');
|
||||
|
@ -1030,11 +1030,11 @@ PersistedModel._defineChangeModel = function() {
|
|||
trackModel: this
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
PersistedModel.rectifyAllChanges = function(callback) {
|
||||
this.getChangeModel().rectifyAll(callback);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle a change error. Override this method in a subclassing model to customize
|
||||
|
@ -1048,7 +1048,7 @@ PersistedModel.handleChangeError = function(err) {
|
|||
console.error(Model.modelName + ' Change Tracking Error:');
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Tell loopback that a change to the model with the given id has occurred.
|
||||
|
@ -1061,6 +1061,6 @@ PersistedModel.handleChangeError = function(err) {
|
|||
PersistedModel.rectifyChange = function(id, callback) {
|
||||
var Change = this.getChangeModel();
|
||||
Change.rectifyModelChanges(this.modelName, [id], callback);
|
||||
}
|
||||
};
|
||||
|
||||
PersistedModel.setup();
|
||||
|
|
Loading…
Reference in New Issue