2013-11-21 3 views
0

부모 생성자에 전달 된 특성을 조정하려면 내 Backbone.Model의 생성자를 재정의하려고합니다. 이것은 내가 뭘하려고하는지입니다 :Backbone.Model 생성자를 재정의 하시겠습니까?

Models.Item = Backbone.Model.extend({ 
    constructor: function (attributes, options) { 
     Backbone.Model.apply(this, this.parse(attributes), options); 
    }, 
    parse: function (attributes) { .... } 
)}; 

내 문제는 부모 생성자가 정의되지 않은 매개 변수로 호출된다는 것입니다.

var Model = Backbone.Model = function(attributes, options) { 
    var attrs = attributes || {}; 
    options || (options = {}); 
    this.cid = _.uniqueId('c'); 
    this.attributes = {}; 
    if (options.collection) this.collection = options.collection; 
    if (options.parse) attrs = this.parse(attrs, options) || {}; 
    attrs = _.defaults({}, attrs, _.result(this, 'defaults')); 
    this.set(attrs, options); 
    this.changed = {}; 
    this.initialize.apply(this, arguments); 
}; 

attributesoptions 인수

https://github.com/jashkenas/backbone/blob/master/backbone.js#L254

는 기본 매개 변수를 사용하여 인스턴스화 모델을 의미하는 모두 정의되지 않습니다.

이제 initialize 메서드를 무시하고 잘못된 특성을 설정 해제하여 올바른 특성으로 바꿀 수 있습니다. 그러나 그것은 단지 해킹처럼 보입니다.

부모 생성자가 정의되지 않은 인수로 호출되는 이유는 무엇입니까?

무엇이 누락 되었습니까?

+0

왜 당신은 이미 답변을 알고있는 질문을 게시하고 있습니까? 질문을 올리면 다른 사람들에게 대답 할 수있는 예의를 갖습니다. – Bart

+0

질문을하는 동안 그것을 알아 냈으므로 그것을 적어두고 싶었습니다. 자신의 대답을 자유롭게 제공하십시오. – Martinffx

답변

0

오류 Function.applyFunction.call

apply의 의미로 수행하는 두 개의 인자 주어진 this 값과 인수의 배열을 기대하고있다.

call

n + 1 인수는, 소정 값 this n 인자 함수에 전달 될 것으로 예상하고있다.
Backbone.Model.apply(this, this.parse(attributes), options); 

은이어야한다 : 나는 정의되지 않은 변수로 리드했다으로

Backbone.Model.call(this, this.parse(attributes), options); 

또는

Backbone.Model.apply(this, [this.parse(attributes), options]); 

apply 함수를 호출.

관련 문제