2016-03-07 113 views
2

我有使用Backbone和RequireJS的問題。骨幹+ RequireJS對象繼承

BaseListView.js

define(['backgrid', 
 
    'backbone', 
 
    'underscore', etc.], function (
 
    Backgrid, 
 
    Backbone, 
 
    _) { 
 
    
 
    return Backbone.View.extend({ 
 
    initialize: function() { 
 
     if(needDeleteCell) { 
 
     this.addDeleteCell(); 
 
     } 
 
     this.render(); 
 
    }, 
 

 
    addDeleteCell: function() { 
 
     var ListViewDeleteCell = DeleteCell.extend({ 
 
     defaults: { 
 
      successMsg: this.attributes.deleteSuccessMsg, 
 
      failureMsg: this.attributes.deleteFailureMsg 
 
     } 
 
     }); 
 
     this.columns.push({ 
 
     name: "delete", 
 
     label: "", 
 
     editable: false, 
 
     cell: ListViewDeleteCell 
 
     }); 
 
    } 
 
    }); 
 
});

ChildView.js

define(['i18next', './common/BaseListView'], function (i18n, BaseListView) { 
 
    'use strict'; 
 

 
    return BaseListView.extend({ 
 
    columns: [{ 
 
     name: "_id", 
 
     label: i18n.t('app.operationForm.id'), 
 
     editable: false, 
 
     cell: "string", 
 
     renderable: false 
 
    }, { 
 
     name: "name", 
 
     label: i18n.t('app.operationForm.nameLabel'), 
 
     editable: false, 
 
     cell: "string" 
 
     }] 
 
    }); 
 
});

現在,如果我想要使用子視圖的多個實例,我有多個「刪除」列(由於BaseListView中的columns.push()),就像父級的columns屬性是單例實例一樣。

在這種情況下,ChildView不延長BaseListView

什麼是Bacbkone + RequireJS這樣做的正確方法是唯一的類?

謝謝。

編輯:這是相同的問題:Switching between singleton and prototype scope using RequireJS但我想避免工廠解決方案。

回答

0

在你的情況ChildView.columns是原型範圍。每個實例都有相同的columns

你可能在initialize功能初始化columns

define(['i18next', './common/BaseListView'], function (i18n, BaseListView) { 
    'use strict'; 

    return BaseListView.extend({ 
    initialize: function() { 
     this.columns = [{ 
     name: "_id", 
     label: i18n.t('app.operationForm.id'), 
     editable: false, 
     cell: "string", 
     renderable: false 
     }, { 
     name: "name", 
     label: i18n.t('app.operationForm.nameLabel'), 
     editable: false, 
     cell: "string" 
     }]; 
     BaseListView.prototype.initialize.call(this); 
    }); 
    } 
}); 
1

ChildView.columns是一個陣列和實例之間共享。你可以從骨幹處理默認值的哈希值的方式中汲取靈感和

  1. 通過設置columns屬性

例如,

ChildView
  • BaseListView陰影定義columns作爲函數這個定義
    var ChildView = BaseListView.extend({ 
        columns: function() { // define `columns` as a function 
         return [{ 
          name: "_id", 
          // ... 
         }, { 
          name: "name", 
          // ... 
         }]; 
        } 
    }); 
    

    and

    var BaseListView = Backbone.View.extend({ 
        initialize: function() { 
         this.columns = _.result(this, 'columns'); 
         this.addDeleteCell(); 
        }, 
    
        addDeleteCell: function() { 
         this.columns.push({ 
          name: "delete", 
          // ... 
         }); 
        } 
    }); 
    

    並演示http://jsfiddle.net/nikoshr/9fk8axpk/1/

  • +0

    感謝您的解決方案,它的工作原理以及@托馬斯-的Jakub-RUP一個,我只是接受了他的解決方案,因爲它需要較少的變化。 – plrenaudin