1

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?

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:

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

1 回答 1

2

是的你可以!!:)

您有 2 个选项。

选项1)

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

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

选项 2)

使用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);
   ...
});
于 2015-03-06T18:37:34.790 回答