javascript - How to access an object/var from view1 but instantiated in view2 in Backbone js? -
i trying access object(this.historycombobox) declared in view(statusview)'s render function , trying access same object view(historyview)'s extendedevents function. have tried use this.historycombobox access unable hold reference. got puzzled. if has got different idea ready try out!
note: statusview gets initialized prior historyview.
following code snippet.
statusview = backbone.view.extend({ init: function() { //some code }, render: function() { this.historycomobox = new sys.combobox(); } } historyview = backbone.view.extend({ template: _.template(historytemplate), init: function() { //some code }, extendedevents: { 'click #refreshbutton': function() { //want access historycomobox; not accessible 'this.historycomobox' } } }
to property of statusview instance, need reference instance. so, if have this:
var statusview = new statusview(); then within methods of historyview, can this:
statusview.historycombobox; however, while can way, wouldn't access statusview instance directly this. better way pass historyview instance parameter, receive in initialize method. keeps views loosely coupled,
var historyview = backbone.view.extend({ initialize: function (options) { this.statusview = options.statusview; }, events: { 'click #refreshbutton': function () { // use this.statusview; } } }); (i notice you're using names init , extendedevents. don't mention you're using third-party library backbone or else might change those, i'll mention backbone expects these initialize , events respectively.)
Comments
Post a Comment