backbone-express-spa icon indicating copy to clipboard operation
backbone-express-spa copied to clipboard

Example of sharing collections or models between apps (=modules, issue #15)

Open joafeldmann opened this issue 11 years ago • 7 comments

How can I listen to events from a collection created in a different "app"? Using a global namespace?

joafeldmann avatar May 10 '13 11:05 joafeldmann

This is a very interesting issue, you touched Joachim :)

While I was giving my talk on SPA's I've been asked about that few time, so people are definitely interesting in that.

My experience so far - do not use the shared state.

Sharing the state, which is commonly done through global namespace as the same anti-pattern as global variables. If application need some kind of data, it have to fetch it from server during loading as pass it to main view.

Consider application as boundary there all data lives, it does not exchange the data with other applications. Furthermore, if you switch from one application to another - all data is gone.

Data could be shared only within application (even not between application and sub-application). And it's done easily, passing the models and collections to views, which are .listenTo events or/and modify data. All other sub-view which are interested in that events, could react accordingly.

I would be really happy to hear for some use cases of where the sharing of data between applications are really useful.

alexbeletsky avatar May 10 '13 13:05 alexbeletsky

Is it possible share data between application and sub-application or it's imposible?

ghost avatar May 13 '13 20:05 ghost

@juansiles I would not say, it's possible. Technically you can do that, by putting some data to global namespace.

What I'm saying is that I see that as wrong practice - each application is responsible for it's own data.

alexbeletsky avatar May 14 '13 11:05 alexbeletsky

I really need to share data between application because I am developing a SPA to reserve tennis paddle. I use one application for reserve and other application for the club bar, and I would like to share data between these application because if a player drinks something from the bar I would like to add this drink in the application to reserve.

Sorry for my English, I know that it's not very good

ghost avatar May 16 '13 10:05 ghost

An example of sharing data between apps could be a user model, which needs to be accessible in each app once the user has logged in.

Even if you try to avoid sharing models/collections between apps, you need a way to share data between multiple views within an app. I think we've the following options:

  • Using a global namespace
  • Using global events (this.listenTo(Backbone, "change", changeCallback)) and pass data as params
  • Initialize the object (model/collection) within each file and load the object via require

joafeldmann avatar May 21 '13 09:05 joafeldmann

@joafeldmann sharing data between multiple views is not a problem. Since the model passed to MainView it could pass model/collection and and sub-views it's own.

Regarding the user - it can be the case. In my experiece, we store not user model but some part of it, required to access API. Typically that kind of API token. In this case I prefer to use sessionStorage and keep data there.

alexbeletsky avatar May 24 '13 09:05 alexbeletsky

Why not use something like this:

'var vent = _.extend({}, Backbone.Events)'

And then:

vent.trigger('someevent', somedata)

vent.on('someevent', handlerFn, this)

ivadenis avatar May 13 '14 03:05 ivadenis