Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joafeldmann opened this issue May 10, 2013 · 7 comments

Comments

@joafeldmann
Copy link

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

@alexbeletsky
Copy link
Owner

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.

@ghost
Copy link

ghost commented May 13, 2013

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

@alexbeletsky
Copy link
Owner

@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.

@ghost
Copy link

ghost commented May 16, 2013

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

@joafeldmann
Copy link
Author

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

@alexbeletsky
Copy link
Owner

@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.

@ivadenis
Copy link

Why not use something like this:

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

And then:

vent.trigger('someevent', somedata)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants