Reputation: 468
Here is my situation. I am collecting a list of members (model = member, collection = members) in my first view. Then I want them to be displayed in my 3rd view. The problem is everytime I want to access memberList, I have to create a new instance, and therefore the data is lost. I am using require.js to modularize the views. How do I effectively access the memberList variable which I had instantiated in the 1st view?
Upvotes: 1
Views: 63
Reputation: 19812
A quick a dirty way is to return new Collection() in your AMD module. Require.js will cache this and every time you require it it will return the same instance Example:
define(function(require){
var Backbone = require('backbone');
return new Backbone.Collection.extend({});
});
I don't recommend this way though cause is very hard to test.
Another way is to have an object APP that you can save references to collections, models etc and then require it in your modules like https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.application.md
Another ways is to have a Mediator that will instantiate the Collection for you and will expose methods from the collection to your other modules. The you require the mediator and not the collection. http://addyosmani.com/resources/essentialjsdesignpatterns/book/#mediatorpatternjavascript
Is up to you, but the less coupling the better.
Hope that helps.
Upvotes: 1