2012-09-04 5 views
9

すべてのビューが拡張するベースビューを作成する必要があります。 このビューをどこでいつ宣言するのかはわかりません。backbone.jsでベースビューを作成するには?

基本的には、すべてのテンプレートにglobal variablesを注入する必要があります。それは、すべてのrender()の方法では行いません。

これが今の私のツリー構造である:

|-main.js 
|-app.js 
|-require.js 
|-App 
| |-View 
| | |-Dashboard.js 
| | |-Header.js 
| | |-Content.js 
| |-Model 
| |-Collection 
| |-Template 
| 
|-Libs 
    |-... 

これは私のapp.js

var App = { 
    ApiURL: "http://domain.local", 
    View: {}, 
    Model: {}, 
    Collection: {}, 
    Registry: {}, 
    Router: null 
}; 

define(['backbone', 'View/Dashboard'], function(Backbone){ 
    var AppRouter = Backbone.Router.extend({ 
     routes : { 
      "dashboard": "index", 
     }, 

     index: function() { 
      console.log('routing index route...'); 
      var x = new App.View.Dashboard({el:$('#main-content'), type:'other'}); 
     } 
    }); 

    var initialize = function() { 
     App.Router = new AppRouter; 
     Backbone.history.start({pushState: true}); 
     console.log('Backbone is running...'); 
    }; 

    return { 
     initialize : initialize 
    }; 
}); 

そして今、私のすべてのビューのための

であるが、このよう Backbone.View継承:

App.View.Dashboard = Backbone.View.extend({ 

私は自分のアプリケーションを作成したいと思っていますBase View ldは伸びる。 これまで行ってきたことですが、app.jsではダッシュボードビューを読み込んでいますので、前に行う必要があるためこのコードを配置する場所はわかりませんが、このベースを必要とする必要がありますすべてのビューでビューオブジェクト...ので、私は迷ってしまいました:(

define(['backbone', 'underscore', 'twig'], function(Backbone, _){ 

    App.View.Base = Backbone.View.extend({}); 
    _.extends(App.View.Base.prototype, { 
     initialize: function(params) 
     { 
      this.el = params.el; 
      this.init(params); 
     }, 

     init: function(params) 
     { 
     }, 

     renderTemplate:function(template_path, data) 
     { 
      var tpl = twig({href:template_path, async:false}); 

      // Inject variables 
      data.user = App.Registry.User; 
      data.account = App.Registry.Account; 

      return tpl.render(data); 
     } 
    }); 

}); 

任意のアイデアや発言は歓迎されている答えは最高次のようになります。D

おかげで、マキシム

答えて

11
App.View.Base = Backbone.View.extend({ 
    baseMethod: function(params) {}; 
}); 

App.ExtendedView.Base = App.View.Base.extend({ 
    // new stuff here 

    // overriding App.View.Base.baseMethod 
    baseMethod: function(params) { 
    // overriding stuff here 
    App.View.Base.prototype.baseMethod.call(this, params); // calling super.baseMethod() 
    } 
}); 
+0

私は他の方法でそれをやってしまったが、この答えは私の質問のための最良のように見える;) – maxwell2022

関連する問題