0

私はnode.jsとsailsの初心者ですが、簡単なので気に入っています:) 実際、MongoDB ans Sails-mongoを使用してsails Framework 0.10rc3を使用しています。

ウォーターラインの貢献者は、mongodb ( https://github.com/balderdashy/sails-mongo/issues/44#issuecomment-35561040 ) のようにモデルに埋め込まれたドキュメントの大ファンではないことを知っていますが、とにかく、方法を知りたかった「this」変数はそれらで機能し、内部配列の現在の要素を取得する方法。

モデルの例を次に示します (これを ProjetSpan と呼ぶことができます)。

module.exports = {

attributes: {

        proj: 
        {
            model:'Projet'
        },

        spans:
        {
            start:
            {
                type: 'DATETIME',
                required: true,
                datetime: true,
                before: function() {
                    if (this.end < this.proj.end)
                        return this.end;
                    else
                        return this.proj.end;
                }
            },

            end:
            {
                type: 'DATETIME',
                required: true,
                datetime: true,
                after: function() {
                    if (this.start > this.proj.start)
                        return this.start;
                    else
                        return this.proj.start;
                }
            }
        }
}

};

この場合、「これ」はどのように機能しますか? 「これ」はスパンですか (したがって、this.end は機能し、this.proj.end は機能しません)、「これ」は ProjetSpan ですか (ans したがって、this.proj.end は機能しますが、this.end は機能しません) ?

最後に、この埋め込みコンテキストで this.end (現在のスパンの変数) と this.proj.end (現在のドキュメントの関連付けの変数) を機能させる方法は?

4

1 に答える 1

1

jsonWaterline は、データ型の提供を除いて、埋め込みドキュメントをまったくサポートしていません。したがって、モデルの例は Sails では機能せず、次のように書き直す必要があります。

module.exports = {

   attributes: {

    proj: {
        model:'projet'
    },

    spans: {
        type: 'json'
    },

    before: function() {

       if (this.spans.end < this.proj.end) {
          return this.spans.end;
       } else {
          return this.proj.end;
       }

    },

    after: function() {

       if (this.spans.start > this.proj.start) {
          return this.spans.start;
       } else {
          return this.proj.start;
       }

    }



}

インスタンス メソッド (beforeおよびafterhere など) では、thisインスタンス オブジェクト全体を参照します。this.projがオブジェクトであること (つまり、 が取り込まれたことProjetSpan.find({}).populate('project'))this.spans.endthis.spans.start実際に存在すること (Waterline は埋め込まれた JSON を検証しないため)を確認するために、そのコードを拡張する必要があります。

于 2014-03-05T17:06:54.697 に答える