Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a static variable in Backbone js views

I am new to Backbone.js. I have this project where there is a hierarchy of multiple views and sometimes the views need to communicate with each other. After a little research on the Internet, I came across https://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/ and tried to use the event aggregator.

However, it didn't work. My guess is that the 'vent' is not the same instance across all the views. So, if there is any way to definine it as a static variable, I can probably make it work. So, is there a way to define static variables in Backbone.js?

like image 919
kosta Avatar asked Jul 21 '26 06:07

kosta


1 Answers

Yes, it is possible to add static variables to any Backbone objects using extend:

obj.extend( protoProps, staticProps );

e.g.:

var MyView = Backbone.View.extend( { events: ... }, {
    myStaticVar: 'anything'
} );
...
var value = MyView.myStaticVar;

However, I am not sure you need this in this case.

like image 166
FERcsI Avatar answered Jul 23 '26 18:07

FERcsI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!