0

I'd like to assign a value to a variable like "down" when network connectivity is lost. However, I'm not sure where and how to set the variable in Backbone so that it is accessible from any view or model. I'm new to Backbone and Marionette. This variable will be useful for modules that are doing a REST endpoint call.

In AngularJS, I can either set an http interceptor or as a service/factory but not sure how to do it in BackboneJS Marionette app.

if (networkStatus !== 'down') {
   // do the backbone.fetch()
}

Also, is my idea a good approach?

devwannabe
  • 3,160
  • 8
  • 42
  • 79
  • Can't you put it in a model that is passed to the views that need it? – Quentin Roy Aug 31 '15 at 16:07
  • I'm new to backbone. When I create a model, can it be used in different views? – devwannabe Aug 31 '15 at 16:08
  • Yes, there is not such limitation in Backbone. However, a view is usually associated with one single model which may not be handy in this case. Other models can still be given as arguments and stored manually though. – Quentin Roy Aug 31 '15 at 16:11

1 Answers1

2

You can use Backbone.Radio, and set up a globalState channel:

var globalStateChannel = Backbone.Radio.channel('globalState')
if(globalStateChannel.request('networkStatus') !== 'down'){ ... }

Somewhere in you App, you should set up the reply handler:

var globalStateChannel = Backbone.Radio.channel('globalState')
globalStateChannel.reply('networkStatus', function(){
  // put your logic here
  return something
}

Of course, explicitly referencing the channel every time is awfully long - you'd do it at Viewss initialisation, but this is food for thought ^^

moonwave99
  • 21,957
  • 3
  • 43
  • 64