15

I'm trying to pass 2 models to the view, but it seems it is not working. Here is my example: http://jsfiddle.net/kahhor/jp4B6/14/ As you can see second alert is showing undefined...

May be I have wrong approach. What I'm trying to do is: in View1 bind event 'change' to Model1... Than by clicking button in View2, call function in Model1 which changes value, and automatically render View1, since it was binded to change event.

But don't forget that View2 has also its own Model2, which I created outside the view and than passed it like new View2({model:Model2});.

It might looked confusing at first, but I think it is simple thing that backbone can do. I just don't know how to do it :)

Thanks,

kaha
  • 1,417
  • 2
  • 17
  • 21

3 Answers3

24

you can access custom parameters (options) from

window.PopupView = new PopupView({ model: LeftNotificationM, model2: PopupM});

like this:

window.PopupView = Backbone.View.extend({

    // code left out

    initialize: function () {
        this.model.bind('change:notification_num', this.render);
        alert(this.model);
        // your model2 option:
        alert(this.options.model2);
    },

   // code left out
});

Conclusion: "unrecognized options" of a view can be found in this.options

sled
  • 14,525
  • 3
  • 42
  • 70
  • Wow, than you it worked. I was stuck with 2 days :) I have just 2 questions. 1. Where can I read more about this.options, I saw it before, but I don't really understand how can I use it... 2. I guess you understand what I'm trying to do. I would like to know if it is good way of doing it? Or there some better (more correct way) of doing it? – kaha Oct 12 '11 at 02:42
  • Hi, 1) I just had a look at the source code [here](http://documentcloud.github.com/backbone/docs/backbone.html#section-119) that's what I love about backbone.js: It's simple :) 2) A model may be attached to multiple views, that's ok. Just spend some attention on the naming "model2" isn't a very good name ;) – sled Oct 12 '11 at 02:50
  • Yes I know, I just showing simple example :) Thank you again :) – kaha Oct 12 '11 at 03:01
  • It doesn't seem that the model2 event binding is working e.g., `model2.on('change:field', this.doSomething, this)` – hoffmanc Nov 13 '12 at 02:06
  • How does this work when trying to template using both models? – streetlight Mar 28 '13 at 13:48
  • http://stackoverflow.com/questions/15683743/template-two-models-in-one-view-backbone-marionette – streetlight Mar 28 '13 at 14:41
6

I just found this question and Sled's answer via Google and it helped me a lot - but just to update this 2 year old question and perhaps save some other Googlers the headache:

Backbone Views no longer automatically attach options passed to the constructor as this.options, but you can do it yourself if you prefer.

Vitaliy's Answer on a similar question shows you how:

initialize : function (options) {
  this.options = options || {};
}
Community
  • 1
  • 1
Joel
  • 1,650
  • 2
  • 13
  • 20
2

Add this patch and pass any options directly to view.

    Backbone.View.prototype._configureWithoutThis = Backbone.View.prototype._configure;
    Backbone.View.prototype._configure = function(options) {
      this._configureWithoutThis(options);
      _.extend(this, this.options);
    }
Alex Craft
  • 13,598
  • 11
  • 69
  • 133