backbone-express-spa
backbone-express-spa copied to clipboard
Example of sharing collections or models between apps (=modules, issue #15)
How can I listen to events from a collection created in a different "app"? Using a global namespace?
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.
Is it possible share data between application and sub-application or it's imposible?
@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.
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
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 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.
Why not use something like this:
'var vent = _.extend({}, Backbone.Events)'
And then:
vent.trigger('someevent', somedata)
vent.on('someevent', handlerFn, this)