2012-09-04 21 views
9

Tüm görünümlerimin uzayacağı bir temel görünüm oluşturmam gerekiyor. Bu görünümü nerede ve ne zaman beyan edeceğime emin değilim.Backbone.js'de temel görünüm nasıl oluşturulur?

Temel olarak, tüm şablonlar için global variables enjekte etmem gerekiyor ve bunu her render() yönteminde kullanmıyorum.

bu şimdilik benim ağaç yapısı şöyledir:

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

bu benim 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 
    }; 
}); 

Ve şimdi bütün görünümü için

olan böyle Backbone.View devralan:

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

Base View kendiminkini oluşturmak istiyorum. Ld uzar. Şimdiye kadar yaptığım budur, ancak bu kod parçasını nereye yerleştireceğimi bilmiyorum çünkü app.js'de Dashboard görünümünü yüklüyorum, bu yüzden daha önce yapmam gerek ama bu tabana ihtiyacım var tüm görünümlerde görünüm nesnesi ... ben kayboldum :(

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); 
     } 
    }); 

}); 

herhangi bir fikir veya açıklamalar bekliyoruz bir cevap iyi olurdu. D

sayesinde Maxime'i

cevap

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

Başka bir yolla bitirdim ama bu cevap benim soru için en iyisi gibi görünüyor;) – maxwell2022

İlgili konular