2012-09-04 10 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 बनाना चाहता हूं जो एप्लिकेशन के सभी विचारों को wou एलडी विस्तारित करता है। यह मैंने अभी तक किया है, लेकिन मुझे नहीं पता कि कोड के इस टुकड़े को कहां रखा जाए क्योंकि 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); 
     } 
    }); 

}); 

किसी भी विचार या टिप्पणी का स्वागत है एक उत्तर सबसे अच्छा होगा:। डी

धन्यवाद, मैक्सिमे

उत्तर

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

संबंधित मुद्दे