2013-09-20 2 views
0

Env : 티타늄 3.1.3, 합금 1.2.2.티타늄 합금 모델 및 컬렉션 URL이 다릅니다

나는 모델/컬렉션에 지속성에 대해 다음 어댑터를 사용하고 있습니다 : https://github.com/viezel/napp.alloy.adapter.restapi

나는 그것이 하나의 모델을보다 콜렉션에 대해 다른 URL 구조를 가지고있는 API가 있습니다. [GET] /files/:id

는 사용자의 모든 파일을 얻으려면 :

가 하나의 레코드 얻으려면 다음을 고려 [GET] /users/:id/files

을 내가 가지고 files.js에 대해 다음 스키마

exports.definition = { 
    config: { 
     "URL": "https://my.api.here/files", 
     //"debug": 1, 
     "adapter": { 
      "type": "restapi", 
      "collection_name": "files", 
      "idAttribute": "id" 
     } 
    },  
    extendModel: function(Model) {  
     _.extend(Model.prototype, {}); 
     return Model; 
    }, 
    extendCollection: function(Collection) {   
     _.extend(Collection.prototype, { 
      initialize: function(){ 
       this.url = "http://my.api.here/users/"+this.user_id+"/files"; 
      } 
     }); 
     return Collection; 
    }  
} 

내가 위의 작업에서 컬렉션의 URL 구조를 변경하려면 컬렉션 초기화 메서드를 재정의해야합니다. 이에 따라 다음과 같이 호출합니다.

var currentUserFiles = Alloy.createCollection("files", {user_id:"12345"}); 
    currentUserFiles.fetch({ 
     success: function(files){ 
      console.log("Woo! Got the user's files!"); 
      console.log(JSON.stringify(files.models)); 
     }, 
     error: function(){ 
      console.log("Nope"); 
     } 
    }); 

이것은 작동하지 않습니다. fetch() 메서드는 계속 /files을 호출하려고 시도합니다. 생성 된 후에도 컬렉션의 속성으로 URL을 설정하려고 시도했으나 작동하지 않습니다. 이상적으로는 로컬 인스턴스와 컬렉션의 싱글 톤 버전 모두에 대해이 작업을 수행하고 싶습니다.

그래서 질문은 : 모델에 대해 수행하는 것보다 컬렉션에 대해 다른 URL을 사용할 수 있습니까? 분명히 나는 ​​단지 /files을 호출하고 클라이언트 쪽 정렬/필터를하고 싶지는 않습니다. 많은 레코드가있는 악몽 일 것입니다. 내가 여기서 무엇을 놓치고 있니?

답변

0

조금 늦었지만 다른 누구에게나이 문제가 발생합니다. 문제는 URL이 모델 및 컬렉션에 지정된/방법입니다. 모델은 하나의 객체 만 될 수 있기 때문에 모델에 전달 된 특정 ID (예 : 기본 키)가 필요합니다. 둘 이상의 오브젝트가 필요하면 콜렉션을 사용하십시오. 희망이 도움이 :)

extendModel : function(Model) { 
    _.extend(Model.prototype, { 
     url : function() { 
      return "http://my.api.here/users/"+this.user_id+"/files/"+ FILE_ID 
     }, 
    }); 
    return Model; 
}, 
extendCollection : function(Collection) { 
    _.extend(Collection.prototype, { 
     url : function() { 
      return "http://my.api.here/users/"+this.user_id+"/files" 
     }, 
    }); 
}, 
관련 문제