2013-05-16 19 views
0

我的應用程序中有一些場景需要在保存數據之前處理數據。在使用geddy保存數據前處理數據

我有一個CakePHP背景,所以我通常會在Model的beforeSave方法中執行此操作。

有什麼等價物,我可以在我的模型中做geddy?

回答

1

查看Model events

Both the base model 'constructors,' and model instances are EventEmitters. The emit events during the create/update/remove lifecycle of model instances. In all cases, the plain-named event is fired after the event in question, and the 'before'-prefixed event, of course happens before.

The 'constructor' for a model emits the following events:

  • beforeCreate
  • create
  • beforeValidate
  • validate
  • beforeUpdateProperties
  • updateProperties
  • beforeSave (new instances, single and bulk)
  • save (new instances, single and bulk)
  • beforeUpdate (existing single instances, bulk updates)
  • update (existing single instances, bulk updates)
  • beforeRemove remove

例如:

var MyModel = function() { ... }; 

MyModel = geddy.model.register('MyModel', MyModel); 

MyModel.on('beforeSave', function(data){ 
    console.log(data); 
}) 
+0

不知道如果我在我的模型正確地實現這個,因爲它會導致User.on( 'beforeSave',函數(){ ^ 類型錯誤:對象功能(){ this.defineProperties({ 電子郵件:{類型: '字符串'}, 密碼:{類型: '字符串'} }); }沒有方法'on' – timstermatic

+0

@wiseguysonly更新了我的答案。我不確定是否該如何讓項目被保存 - 因此在beforeSave事件中,請嘗試記錄'this'或'arguments'來查看您要操縱的項目的位置。讓我知道你的進步:) – JAM

+0

謝謝你的工作。我編輯了你的答案,將數據參數添加到匿名函數中。有了這個,我可以然後做data.password等 – timstermatic

相關問題