2014-03-05 1 views
0

저는 node.js와 돛에 새로운 것 같습니다. 그렇기 때문에 쉽게 마음에 듭니다. :) 실제로 돛과 함께 Framework 0.10rc3과 MongoDB와 sails-mongo를 함께 사용하고 있습니다.임베디드 문서의 모델에 대한 'this'변수는 어떻게됩니까?

mongodb (https://github.com/balderdashy/sails-mongo/issues/44#issuecomment-35561040) 모델에서 워터 라인의 참여자가 모델에 포함 된 문서의 큰 팬이 아니라는 것을 알고 있지만 어쨌든이 'this'변수가 어떻게 작동하는지, 그리고 현재 요소를 검색하는 방법을 알고 싶었습니다. 내부 배열.

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; 
       } 
      } 
     } 
} 

}; 

이 어떻게 '이'이 경우에 작동합니다 : 여기

모델의 exemple (우리가 ProjetSpan를 호출 할 수있는)를인가? 'this'는 span이기 때문에 this.end는 작동하지 않고 this.proj.end가 아니거나 ProjetSpan (this.proj.end가 작동하지만 this.end는 작동하지 않습니다)입니까?

마지막으로 this.end (현재 범위의 변수) 및 this.proj.end (현재 문서의 연결의 변수)를이 포함 된 컨텍스트에서 사용하는 방법은 무엇입니까?

답변

1

워터 라인은 json 데이터 형식을 제공하는 것을 제외하고는 포함 된 문서를 전혀 지원하지 않습니다. 그래서, 모델의 예는 항해에서 작동하지 않습니다, 뭔가 등 다시 작성해야합니다 :

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; 
     } 

    } 



} 
인스턴스 메소드에서

(같은 beforeafter 여기를) this는 전체 인스턴스 객체를 참조합니다. 수표로 this.proj이 객체 (즉, ProjetSpan.find({}).populate('project')으로 채워 졌음)가되고, this.spans.endthis.spans.start이 실제로 존재하는지 (워터 라인에서 포함 된 JSON의 유효성을 검사하지 않기 때문에) 해당 코드를 향상시키는 것이 좋습니다.

+0

고맙습니다. 객체 검사의 경우 this.proj! = null && typeof this.proj === 'object''를 작성했지만'span '은 무엇입니까? 이 상태에서 객체, 배열 또는 다른 것이 있습니까? – DestyNova

+0

"spans"는 사용자가 설정 한 JSON 유형이됩니다. 객체 또는 배열 일 가능성이 큽니다. 안타깝게도 그것들은 모두'typeof' "object"이지만,'sails.util.isArray()'를 사용하여 배열을 테스트 할 수 있습니다 ('sails.util'은 Lodash를 래핑합니다). – sgress454

+0

쿨 thx :)! 권장 사항을 따르기 위해 모델을 변경했지만 어떤 시점에서는 딥 연관성 테스트가 어떻게됩니까? if (this.projCollab! = null && typeof this.projCollab === 'object') && (this.projCollab.proj! = null && typeof this.projCollab.proj === 'object')) {if (this.end DestyNova