2012-03-02 9 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' }) 
関連する問題