wowpatrick
wowpatrick

Reputation: 5180

Backbone.js - Build view with sub models

I have an edit view for a Backbone Model that I create each time the the element is clicked on. The problem I have is that the edit view needs two Backbone collections to create the edit form (it contains two <select> lists).

The view:

MyApp.elementView = Backbone.View.extend({
    events: {
            'click .edit': 'editForm',
    },
    editForm: function(ev) {

            var editView = new TimeTrack.Views.EditJob({
                model: this.model
                // This view needs two more collections
                // for the <select> elements
            });
            ...
    }
});

Instantiate the view:

var elementView = new MyApp.elementView({
        collection: elementCollection
});

What is the best way to push the needed collections to the edit view? Do I have to pass the collections need for the edit view from the elementView form the instantiation? Or is there a better way of doing this?

Upvotes: 1

Views: 167

Answers (1)

xdsemx
xdsemx

Reputation: 1209

I did so, passed in view 2 collections, 1 - the main and the other as follows: to elementView - second collection and in elementView recive her.

example: in router I'm

initialize: ->
  (YourNameSpace).secondCollection = new (YourNameSpace).secondCollection

 elements: =>
    view = new (YourNameSpace).elementView( secondCollection: @secondCollection )
    $('.l-yield').html(view.render().el)

Upvotes: 3

Related Questions