2017-02-28 3 views
1

두 개의 실제 필드를 연결하여 만든 모델의 이름 필드를 가상으로 만들어야합니다. 이 이름은 표시 전용입니다. 나는 문서에서 가상 예제를 시도했지만 행운이 없다. 키스톤 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

자세한 정보를 제공해 주시겠습니까? 현재 어떤 작업을하고 있습니까? 어떻게 작동해야합니까? 샘플 코드?

편집 :

모델 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

당신은이 작업을 수행하는 가상 필요하지 않습니다. 키스톤을 사용하면 문서를 저장할 때마다 필드를 추적하고 다시 계산할 수 있습니다. 이러한 옵션을 사용하면이 두 값을 연결하는 함수를 만들 수 있습니다 (동기식 또는 비동기식 중에서 선택).

내가 알아챈 또 다른 한 가지는 barRelationship이며 이는 사용자가 유용한 정보를 빼내기 전에 관계를 채우십시오. 즉, value 함수가 비동기 적이어야 함을 의미합니다. 콜백 함수를 해당 함수의 인수로 전달하는 것만 큼 간단합니다. Keystone이 나머지 작업을 수행합니다. 이 bar의 정보가 필요없고 _id (모델에있는 경우에만 필요함)이 필요하면 포함 된 keystone.list('Bar') 기능없이 수행 할 수 있습니다. 이 동적으로 계산됩니다하지만 당신은, 어떤 시나리오에서 모델에 fooname 속성을해야합니다 있도록

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

map 객체는, 모델의 옵션을 의미합니다.

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();