2013-02-10 166 views
4

我試圖擴展骨幹集合,第一種方法是我在聲明中做了新的事情,第二種方法是先聲明,然後創建了一個新實例。做錯了第一件事,有什麼不同?定義一個新的骨幹集合

var AppointmentList = new Backbone.Collection.extend({ 
    model: Appointment 
}); 

var AppointmentList = Backbone.Collection.extend({ 
    model: Appointment 
}); 

var aptlist = new AppointmentList(); 

回答

7

第一個將打破

var Appointment = Backbone.Model.extend({ 
    defaults: { 
     "time": "0000", 
     "note": "This is an appointment" 
    } 
}); 


var AppointmentList = new Backbone.Collection.extend({ 

    model: Appointment 

}); 

var aptlist = new AppointmentList(); 

在Backbone.js的,我們有

var extend = function(protoProps, staticProps) { 

    var parent = this; 
    var child; 


    if (protoProps && _.has(protoProps, 'constructor')) { 
     child = protoProps.constructor; 
    } else { 
     child = function(){ return parent.apply(this, arguments); }; 
    } 

    _.extend(child, parent, staticProps); 


    var Surrogate = function(){ this.constructor = child; }; 

    Surrogate.prototype = parent.prototype; 

    child.prototype = new Surrogate; 


    if (protoProps) _.extend(child.prototype, protoProps); 


    child.__super__ = parent.prototype; 

    return child; 

    }; 

如果實例Backbone.Collection.extend與運營商new,那麼var parent = this將涉及延長對象,但如果你不使用new然後var parent = this將參考Backbone.Collection因爲你只能在函數調用.apply,代碼將打破在這裏:

child = function(){ return parent.apply(this, arguments); }; 

parent會一個東西。 Backbone.Collection是一個功能