Nalini Wanjale
Nalini Wanjale

Reputation: 1757

Update values passed to Backbone view

In addone i m creating new object of view like that

this.section = "aaa";
var sectionview = new AA(model:this.model,section:this.section);

section is global variable of another view that i m passing to AA view.But after passing section its value get change at end of add one like this

this.section = "aaa";
var sectionview = new AA(model:this.model,section:this.section);
.
.
.
.
.
.
this.section = "sss";

then how i can update value of section that i passed at time of creation of view AA??? Expected answer is

this.options.section = "sss" not "aaa"

in AA view

Upvotes: 1

Views: 72

Answers (1)

mu is too short
mu is too short

Reputation: 434665

The usual approach to this sort of thing is to extend Backbone.Events to build a global pub/sub event dispatcher:

window.pub_sub = _({}).extend(Backbone.Events);

Then your view could listen for events from pub_sub:

initialize: function() {
    this.listenTo(pub_sub, 'change:section', this.section_changed);
    //...
},
section_changed: function(section) {
    this.section = section;
    // And whatever else needs to happen...
}

Then trigger the event when you change the section:

pub_sub.trigger('change:section', new_section_value);

You'd want to funnel all changes to the global section through a single function call somewhere to ensure that the events are triggered but you should be doing that sort of thing anyway.

Demo: http://jsfiddle.net/ambiguous/rPtfS/

If you need these settings to persist then change pub_sub to a global settings model and use the usual model persistence mechanisms.

Upvotes: 3

Related Questions