2013-07-01 3 views
1

저는 Backbone보기의 간단한 상속 계층 구조를 가지고 있으며 내 모든보기에 하나의 일반 모델이 있어야합니다. 이러한 뷰에서 모델의 url 속성 만 달라집니다. 그래서 모델을 초기화 할 때 url을 전달합니다.url 속성을 모델에 전달하면 작동하지 않습니다.

GenericView = Backbone.View.extend({ 
template: "friendsList-template.html", 
initialize: function(){ 
    var that = this; 
    that.model.fetch({ 
     success: function(model,response){ 
      that.handleResponse(response); 
     }, 
     error: function(){ 
      //handle error 
     } 
    }); 
} 
}); 

그리고 아이 뷰는 다음과 같습니다 :

부모 일반적인 전망이다

PersonView = GenericView.extend({ 
el:$("#container"), 

personData: {}, 
template: "profile-template.html", 
model = new GenericModel({ 
    url:"testRouter.php" 
});//here is where I'm defining the url 
initialize: function(){ 
    PersonView.__super__.initialize.apply(this); 
}, 
render: function(){ 
    var that = this; 
    $.get(this.templatesPath + this.template, function(resTemplate){ 
     var html = Mustache.render(resTemplate, that.personData); 
     that.$el.html(html); 
    }); 
}, 
handleResponse: function(res){ 
    this.personData = res; 
    this.render(); 
} 
}); 

그리고 GenericModel는

GenericModel = Backbone.Model.extend({ 
}); 

(지금 이상에 대한) 단순히 비어있는 모델입니다 콘솔에서 다음 오류가 발생합니다 :

Uncaught Error: A "url" property or function must be specified backbone.js:1559 

내 계층 구조가 문제입니까? 이견있는 사람? fine manual에서

+0

, 나는이 일을 시도하고 편집 할 수 있었다 및 코드의 잘못된 버전을 붙여 –

답변

1

:

constructor/initializenew Model([attributes], [options])
[...]
{url: "..."} and/or {urlRoot: "..."} options may be passed when creating a new model that needs to have a custom one-off URL endpoint.

참고 두 가지 :

  1. 생성자에 첫 번째 인수는 속성이 객체입니다.
  2. url은 두 번째 options 개체에 있습니다.

그래서 당신은이 일을해야합니다

var model = new GenericModel(null, { 
    url: "testRouter.php" 
}); 

당신이 모델을 만들 때 url을 설정하려는 경우.

데모 : http://jsfiddle.net/ambiguous/gSYug/

관련 문제