DerbyJS 0.6中的服务器端模型更改事件

时间:2022-01-14 15:25:01

In DerbyJS:

How do I add event listeners to "The Model", only server side, without having to rely on requests (express middleware)?

如何将事件监听器添加到“模型”,只有服务器端,而不必依赖请求(表达中间件)?

Related question I've found: How to create server-side application logic on Racer / DerbyJS?

相关问题我发现:如何在Racer / DerbyJS上创建服务器端应用程序逻辑?

Of course I can write an express middleware. But the "shared" data model would come in really handy if I could access it without having to rely on a request, and I don't know how to do that without having to put the specific code into the shared codebase.

当然我可以写一个快速中间件。但是,如果我可以在不必依赖请求的情况下访问它,那么“共享”数据模型将非常方便,而且我不知道如何在不必将特定代码放入共享代码库的情况下执行此操作。

Use case: order processing for e-commerce software

My model could be like this:

我的模型可能是这样的:

{
  orders: [ {
    id: '1',
    products: [ ... ],
    user: [ ... ],
    token: 'stripe credit card token I get from Client here'
  }, {
    ...
  } ]
}

This way the client could place new orders doing

这样客户可以下新订单

model.at('orders').add(myOrder);

And the server could process it by listening for 'insert' events:

服务器可以通过监听'insert'事件来处理它:

model.on('insert', 'orders', function(captures, index, newOrders) {
  newOrders.forEach(processOrder);
})

1 个解决方案

#1


2  

Yes you can!! :)

是的你可以!! :)

You have 2 options.

你有2个选择。

Option 1)

Use app.serverUse to include a server side script, or use the store.js if you have used yo derby-generator. There you can :

使用app.serverUse包含服务器端脚本,或者如果您使用过yo derby-generator则使用store.js。在那里你可以:

var model = dbStore.createModel();
model.subscribe("orders", function(){
  model.on("insert", "orders.**", function() {
   ...
  });
});

Option 2)

Using https://github.com/derbyparty/derby-hook

dbStore.hook('create', 'orders', function(id, doc, session, backend) {
  var model = dbStore.createModel();
  var $order = model.at('orders.'+ id);
   ...
});

#1


2  

Yes you can!! :)

是的你可以!! :)

You have 2 options.

你有2个选择。

Option 1)

Use app.serverUse to include a server side script, or use the store.js if you have used yo derby-generator. There you can :

使用app.serverUse包含服务器端脚本,或者如果您使用过yo derby-generator则使用store.js。在那里你可以:

var model = dbStore.createModel();
model.subscribe("orders", function(){
  model.on("insert", "orders.**", function() {
   ...
  });
});

Option 2)

Using https://github.com/derbyparty/derby-hook

dbStore.hook('create', 'orders', function(id, doc, session, backend) {
  var model = dbStore.createModel();
  var $order = model.at('orders.'+ id);
   ...
});