2012-03-02 45 views
1

從localStorage覆蓋的方法恢復我的集合(下例中的getLabel())不再被調用。基本方法被調用。我認爲問題是,我告訴集合使用BaseModel。但如何更改集合以使用KeywordLog和CommentLog模型?backbone.js - 從localStorage恢復後沒有模型繼承

我用下面的模型繼承:

var BaseLog = Backbone.Model.extend({ 
    defaults: { 
     timecode: null, 
     color: null, 
     isRange: false, 
    }, 
    getLabel: function() { 
     return 'base'; 
    } 
}); 

var KeywordLog = BaseLog.extend({ 
    defaults: _.extend({}, BaseLog.prototype.defaults, { 
     keyword: null,  
    }),  
    getLabel: function() { 
     return 'keyword'; 
    } 
}); 

var CommentLog = BaseLog.extend({ 
    defaults: _.extend({}, BaseLog.prototype.defaults, { 
     text: null,  
    }), 
    getLabel: function() { 
     return 'comment'; 
    } 
}); 

var Loglist = Backbone.Collection.extend({ 
    // This might be the problem after restoring drom localStorage..? 
    model: BaseLog, 
    localStorage: new Store("storage") 
});  

回答

0

集合將只使用一個單一的模型類型工作。我建議你切換到單個模型,並將label作爲屬性。

var Log = Backbone.Model.extend({ 
    defaults: { 
     timecode: null, 
     color: null, 
     isRange: false, 
     label: 'base', 
     text: null 
    }, 
    initialize: function() { 
     _.bindAll(this); 
    }, 
    getLabel: function() { 
     return this.label; 
    } 
}); 

log = new Log; 
log.set({ text: 'keyword here', label: 'keyword' }) 
log2 = new Log; 
log2.set({ text: 'comment here', label: 'comment' })