Page Contents

概要

主要的LoopBack JavaScript对象是:

如果在你的代码里面得到这些对象呢?

Getting the app object

获得app对象非常重要,因为通过app对象你可以获得其它的对象(如models和data source)。通常你想在下面的脚本中获得app对象:

  • Model脚本: /common/models/_modelName_.js (modelName就是你模型的名字)
  • 启动脚本在 /server/boot 目录下
  • 中间件(the ones you register in boot scripts and the ones in /server/server.js)

  • 你自己的自定义脚本

在启动脚本中获得

为了在启动脚本中获得app对象,你只需要把它放在导出函数的第一个参数即可。

异步启动脚本有一个callback函数:

Asynchronous boot script - /server/boot/your-script.js

module.exports = function(app, cb) { //app会被LoopBack自动注入进来
  ...
};

同步启动脚本没有callback函数:

Synchronous boot script - /server/boot/your-script.js

module.exports = function(app) { //app会被LoopBack自动注入进来
  ...
};

通过上面的例子可以看出来,LoopBack自动提供了app对象做为启动脚本的第一个参数。

在中间件中获得

在中间件里面LoopBack自动把app对象赋给了request对象 (actually, under the hood, Express does it),  你可以在server/server.js 里面看到如下代码:

Middleware - /server/server.js

...
app.use(function(req, res, next) {
  var app = req.app;
  ...
});
...

在自定义脚本中获得

如果你想在你的自定义脚本中使用app对象,你只需要简单的require’/server/server’就可以了:

A custom script - /server/your-script.js

var app = require('/server/server');
...

在模型脚本中获得

可以直接require /server/server 获得app对象:

Model - /common/models/book.js

var app = require('../../server/server'); //require `server.js` as you would normally do in any node.js app

module.exports = function(Book) {
  ...
};

可以使用model.app获得app对象:

...
Book.app
...

注意了, 有种情况你并不能通过model.app获得app对象,因为只有等bootstrapping结束了这个model文件被添加到了app的属性中,你才能够通过model.app获得app对象。这意味你不能想下面的代码中这样做:

CANNOT do this in a model script

module.exports = function(Book) {
  Book.app... //不起作用,因为 `.app` 还没有被添加到Book对象中
});
但是你可以在远程方法,远程钩子里面获得.app,因为他们都发生在应用加载结束之后 (发生在loopback.boot运行之后 发生在 /server/server.js 调用boot(..)之后)。这意味着下面的代码是可以成功获得app的:
module.exports = function(Book) {
  Book.read(cb) {
    var app = Book.app;
    console.log(app.models...)
    cb();
  };
  Book.remoteMethod(
    'read',
    ...
  });
};

Of course, you can do the same in remote hooks and remote methods, but be aware of the load timing. Simply put, model.app will not be available until the application has completed bootstrapping, that is run boot() in /server/server.js. The idea here is to define our models before they are added to the application. 一旦应用bootstrapping结束, 你可以得到model的app属性。

REVIEW COMMENT from simon
We should use var app = require('../../server/app');. However, this will require renaming in various parts of the app that might have huge side effects.</div>

REVIEW COMMENT from bajtos

我们不建议使用 var app = require('../../server/server') 因为当同一个model同时被server和client使用的时候它就不能很好的工作了。获得app对象最简单的方法是通过Model.on(‘attached’)事件。

<div class="codeContent panelContent pdl"><pre class="theme: Emacs; brush: jscript; gutter: false" style="font-size:12px;">module.exports = function(MyModel) {   var app;   MyModel.on('attached', function(a) {
app = a;
// perform any setup that requires the app object   }); };</pre></div>

使用app对象

LoopBack app对象定义在主脚本中,如下:

/server/server.js

var loopback = require('loopback');
var app = loopback();

app对象扩展自Express app object; 并继承了它所有的属性和方法。

使用模型对象

获得模型对象

一旦你获得了app对象,你可以通过app的models属性轻易得到你想要的模型对象。

Boot script - /server/boot/your-script.js

module.exports = function(app) {
  var app = app.models.Book;
  ...
};

在你的自定义脚本中:

A custom script - /server/your-script.js

var app = require('/server/server');

使用模型对象

Basic models

Overview

By default, a LoopBack model has properties and methods “mixed in” from:

When you define relations between models, the RelationMixin object object also gets mixed in to the model object.

Events

The following table summarizes the events that LoopBack models can emit.

Event Emitted when... Arguments Argument type Class methods that emit Instance methods that emit
'attached'

Model is attached to an app.

 


Model class Object app.model(modelName)  
'changed' Model instance is created, saved, or updated.  Model instance Object
  • Model.create()

  • Model.updateOrCreate() 

  • Model.upsert() 

  • Model.prototype.save()

  • Model.prototype.updateAttributes()

'dataSourceAttached' Model is attached to a Data source. Model class Object  
  • DataSource.prototype.createModel  
  • DataSource.prototype.define
'deleted' Model instance is deleted. Model ID Number
  • Model.removeById()
  • Model.destroyById()
  • Model.deleteById()
  • Model.prototype.remove()
  • Model.prototype.delete()
  • Model.prototype.destroy()
 'deletedAll' Model instance is deleted. where (optional) JSON object
  • Model.remove()

  • Model.deleteAll()

  • Model.destroyAll()

 
'set' Model property is set. Model instance Object  

Model.prototype.setAttributes()

Connected models

In addition to the methods of the Basic model object, the following are mixed in when a model is connected to a data source:

使用data source对象

获得data source对象

和通过app获得model类似,你可以通过app.datasources获得data source对象:

Boot script - /server/boot/your-script.js

module.exports = function(app) {
  var dataSource = app.datasources.db; //db can be any registered datasource in `/server/datasources.json`
  ...
};

在你的自定义脚本中:

A custom script - /server/your-script.js

var app = require('./server/server');
...
var datasource = app.datasources.db;
...

在中间件中:

Middleware - /server/server.js

...
app.use(function(req, res, next) {
  var dataSource = app.datasources.db;
  ...
});
...

在模型中:

Model - /common/models/model.js

module.exports = function(Book) {
  Book.read = function() {
    var dataSource = Book.app.datasources.db;
  };
  Book.remoteMethod(
    'read',
     ...
  );
};

注意了在模型中,下面的代码代码不能获得app对象:

Model - /common/models/model.js

module.exports = function(Book) {
  Book.app... //`Book` 还没有被注册进来!获取不到app对象。
};

使用data source对象