2012-04-03 3 views
0

초기화 기능에 따라 URL이 달라지는 백본 컬렉션이 있습니다. 이 Backbone 컬렉션의 인스턴스를 만들 때 모델의 어떤 인스턴스가 나타나는지 필터링하기 위해 ID를 전달합니다. 컬렉션의 코드는 다음과 같습니다.백본 컬렉션의 URL은 초기화 기능에 따라 다릅니다.

var GoalUpdateList = Backbone.Collection.extend({ 

    // Reference the Goal Update model 
    model: GoalUpdate, 

    // Do HTTP requests on this endpoint 
    url: "http://localhost:8000/api/v1/goal_update/?goal__id=" + this.goal_id + "&format=json", 

    // Set the goal ID that the goal update list corresponds to 
    initialize: function(goal_id) { 
     this.goal_id = goal_id; 
     console.log(this.goal_id); 
     console.log(this.url); 
    }, 

    }); 

물론 이것은 작동하지 않습니다. this.goal_id은 정의되지 않은 것으로 간주됩니다. URL 초기화 함수를 실행하기 전에 설정되어 있기 때문에 같아요.

답변

4

url에 동적으로 URL을 작성하는 기능을 사용할 수 있습니다.

url: function() { 
    return "http://localhost:8000/api/v1/goal_update/?goal__id=" + this.goal_id + "&format=json"; 
}, 
관련 문제