2017-02-28 8 views
1

2つの実フィールドを連結して作成したモデルの名前フィールドを仮想にする必要があります。この名前は表示専用です。私は文書で仮想の例を試しましたが、運はありません。キーストーン4ベータ5。バーチャルな「名前」フィールド?

var keystone = require('keystone') 
    _ = require('underscore'); 
var Types = keystone.Field.Types; 

/** 
* Foo Model 
* ================== 
*/ 

var Foo = new keystone.List('Foo', { 
     map: {name: 'fooname'}, 
     track: true 
}); 

Foo.add({ 
     bar: { type: Types.Relationship, required: true, initial: true, label: 'Barref', ref: 'Bar', many: false }, 
     order: { type: Types.Select, required: true, initial: true, label: 'Order', options: _.range(1,100) }, 
     price: { type: Types.Money, format: '$0,0.00', label: 'Price', required: true, initial: true }, 
}); 

Foo.schema.virtual('fooname').get(function() { 
     return this.bar+ ' ' + this.order; 
}); 

Foo.defaultColumns = 'fooname, bar, order, price'; 
Foo.register(); 

このモデル定義を使用すると、デフォルトの列リストに仮想名が表示されません。私はバーチャルネームを作りたいので、このモデルを関係として使うと検索が楽になります。

答えて

-1

詳細をお知らせください。現在何が働いていますか?どのように動作するのでしょうか? サンプルコード?

EDIT:

モデルFooを作成した後、属性Foo.schemaを使用してマングーススキーマにアクセスすることができます。 (Keystone Concepts

このschemaは、フックを登録したすべてのメソッドに対してpre -hookを提供します。 (Mongoose API Schema#pre

これらの方法の一つは、このように使用することができ、saveです:

Foo.schema.pre('save', function(next){ 
    console.log('pre-save'); 
    next(); 
}); 
+0

モデルのコードサンプルと詳細な説明を追加するための初期投稿を編集しました。 thx –

-1

はこれを試してみてください。

Foo.schema.pre('save', function (next) { 
    this.name = this.bar+ ' '+ this.order; 
    next(); 
}); 
+1

これは著者の問題を解決するかもしれませんが、コードのみの回答は実際の学習にほとんど役立ちません。あなたは*なぜ*これが答えであるかを説明するかもしれません。 –

0

あなたがこれを行うには、仮想の必要はありません。キーストーンを使用すると、ドキュメントを保存するたびにフィールドを追跡して再計算できます。これらのオプションを有効にして、これらの2つの値を連結する関数を作成することができます(同期または非同期のいずれかを選択します)。

barRelationshipです。有用な情報を取得する前にその関係を設定してください。つまり、value関数は非同期でなければならず、コールバック関数をその関数の引数として渡すだけで簡単です。キーストーンは残りをします。このbarの情報が不要で、_id(モデルには常にある)が必要な場合は、付属のkeystone.list('Bar')機能を使用せずに行うことができます。

http://keystonejs.com/docs/database/#fields-watching

mapオブジェクトは、お使いのモデルでオプションを指し、それが動的に計算されますけれども、あなたは、どのシナリオでモデルにfooname属性が必要になります。

var keystone = require('keystone'), 
    _ = require('underscore'); 
var Types = keystone.Field.Types; 

/** 
* Foo Model 
* ================== 
*/ 

var Foo = new keystone.List('Foo', { 
     map: {name: 'fooname'}, 
     track: true 
}); 

Foo.add({ 
     fooname: { type: Types.Text, watch: true, value: function (cb) { 
      // Use this if the "bar" that this document refers to has some information that is relevant to the naming of this document. 
      keystone.list('Bar').model.findOne({_id: this.bar.toString()}).exec(function (err, result) { 
       if (!err && result) { 
        // Result now has all the information of the current "bar" 
        // If you just need the _id of the "bar", and don't need any information from it, uncomment the code underneath the closure of the "keystone.list('Bar')" function. 
        return cb(this.bar.name + " " + this.order); 
       } 
      }); 
      // Use this if you don't need anything out of the "bar" that this document refers to, just its _id. 
      // return cb(this.bar.toString() + " " + this.order); 
     } }, 
     bar: { type: Types.Relationship, required: true, initial: true, label: 'Barref', ref: 'Bar', many: false }, 
     order: { type: Types.Select, required: true, initial: true, label: 'Order', options: _.range(1,100) }, 
     price: { type: Types.Money, format: '$0,0.00', label: 'Price', required: true, initial: true }, 
}); 

Foo.defaultColumns = 'fooname, bar, order, price'; 
Foo.register(); 
関連する問題