2014-11-22 4 views
1

루프백 푸시 구성 요소에 대해 이야기하고 있습니다. "설치"모델의 "작성"방법을 가로 채려고합니다. 내 코드는 다음과 같습니다 -루프백 이상한 동작

서버/부팅/I 처음을 의미,

module.exports = function (app) { 
var Installation = app.models.Installation; 
var create = Installation.create; 

Installation.create = function (data, cb) { 
    //reinitializing old implementation 
    this.create = create; 
    console.log("Received data: "+JSON.stringify(data)); 

    if (!data || !data.imei) { 
     console.log("No data or imei was provided, creating new"); 
     this.create(data, cb); 
     return; 
    } 

    //saving 'this' reference 

    var that = this; 
    //search by imei filter 
    var filter = {where: {imei: data.imei}}; 
    this.findOne(filter, function (err, result) { 
     if (err) { 
      console.log("Error occurred while looking for installation by IMEI"); 
      cb(err); 
      return; 
     } 
     if (!result) { 
      console.log("No installation found by IMEI, will create a new installation"); 
      that.create(data, cb); 
      return; 
     } 

     console.log("Found existing installation with id: " + JSON.stringify(result)); 

     result.deviceToken = result.gpsLocation = result.osVersion = result.vendor = result.phoneNumbers = null; 

     if (data.deviceToken) { 
      result.deviceToken = data.deviceToken; 
     } 
     if (data.gpsLocation) { 
      result.gpsLocation = data.gpsLocation; 
     } 
     if (data.osVersion) { 
      result.osVersion = data.osVersion; 
     } 
     if (data.vendor) { 
      //result.vendor=data.vendor; 
      result.vendor = 'jahid'; 
     } 
     if (data.phoneNumbers) { 
      result.phoneNumbers = data.phoneNumbers; 
     } 
     that.upsert(result, cb); 
    }); 
    } 
} 

불행하게도이 코드는 한 번만 호출됩니다 installationex.js. 그 후이 코드는 호출되지 않습니다. 로그를보고 확신하게되었습니다. 처음으로 로그 만 인쇄합니다. 그 후에는 로그를 인쇄하지 않습니다.

이 접착제 코드가 한 번만 호출되는 이유는 무엇입니까? 내 의도는 설치 모델에 대한 모든 create 메소드 호출을 인터셉트하는 것입니다. 제공된 "IMEI"에 대한 항목이 이미 있는지 확인한 다음 다시 사용하십시오. 그렇지 않으면 새로 작성하십시오.

미리 감사드립니다.

안부,

Jahid

답변

0

부팅 sc ript는 응용 프로그램 시작 중에 한 번만 실행됩니다. 함수가 호출 될 때마다 트리거하는 함수를 원하면 원격 후크 또는 모델 후크를 사용하십시오. 아마의 라인을 따라 뭔가 :

... 
Installation.beforeRemote('create', ... 
... 

안드로이드 측면에서 더 많은 정보를 원하시면

+0

안녕하십니까,이 링크에 따라 http://docs.strongloop.com/display/public/LB/Customizing+models (페이지 하단에 있음)은 내장 방법을 오버로드 할 때 권장되는 방법입니다. –