var Page = new Schema({
author: {
first_name: String
, last_name: String
}
});
Page.virtual("author.full_name").get(function() {
return this.author.first_name + " " + this.author.last_name;
});
// Later
myPage = new Page({author: {first_name: "John", last_name: "Doe"}});
myPage.author.full_name; // == undefined
myPage.get("author.full_name"); // == "John Doe"
μ체 κ°μμΌλ‘ μμ±μλ₯Ό μν μ μ€ν€λ§λ₯Ό λ§λ€λ €κ³ μλνμ§ μμμ΅λλ€. μλν μλ μμ§λ§ λ€λ₯Έ λ¬Έμ λ³΄κ³ μμ λ°λ₯΄λ©΄ νμ μ€ν€λ§λ μΌλ° νμ λ¬Έμκ° μλ λ°°μ΄ atmμμ μ΅μ μΌλ‘ μλν©λλ€.
μμ€λ₯Ό 보면 μ€ν€λ§ νΈλ¦¬μ κ°μμ μΆκ°νλ λμ μ리ν split(".") λ Όλ¦¬κ° λλ½λμ΄ λΆκ°λ₯ν΄ λ³΄μ΄μ§ μμ΅λλ€.
get λ©μλλ λΉλΆκ° μ μλνμ§λ§ λ€λ₯Έ κ΅¬λ¬Έμ΄ λ μ’μ΅λλ€. :)
λ¬Όλ‘ μ΄ μμ μ¬νμ νμ λ¬Έμμ λ©μλμλ μ μ©λμ΄μΌ ν©λλ€.
μμ±μκ° λ°°μ΄μ΄λ©΄ μ΄λ»κ² λ©λκΉ? μ΄ μ΄λ μ΄ κ°κ°μμ κ°μ μμ μ μνν μ μμ΅λκΉ?
μ΄λ μ΄μ λν +1 μ§μ
μ΄λ μ΄μ λν +100 μ§μ
κ·Έκ²μ μμ λμ΅λλ€ :.
var Variation = new Schema({
label: {
type: String
}
});
// Virtual must be defined before the subschema is assigned to parent schema
Variation.virtual("name").get(function() {
// Parent is accessible
var parent = this.parent();
return parent.title + ' ' + this.label;
});
var Product = new Schema({
title: {
type: String
}
variations: {
type: [Variation]
}
});
μμ μ루μ μ μλνμ§λ§ μ€μ²© μ€ν€λ§μ λν΄ κ°μμ μ€μ ν΄μΌ ν©λλ€.
Variation.set('toJSON', {
virtuals: true
});
κ°μ₯ μ μ©ν λκΈ
μμ μ루μ μ μλνμ§λ§ μ€μ²© μ€ν€λ§μ λν΄ κ°μμ μ€μ ν΄μΌ ν©λλ€.