Search code examples
backbone.jsbackbone-viewsbackbone-eventsbackbone-routing

backbone.js pass param from router to views


Below is my router function where i have an id as the parameter.My question is how to pass the id into the camp views..Thanks..i tried the following..When i do a alert in views it says undefined

Router.js

editcampaign :function(id){
        $('#content').empty();
        require([ 'views/camp' ], function(editcampaign) {
            $('#content').html(new editcampaign({campaignID:id}).render().$el);
        });
    },

Views.js

  SM.Views.editCampaign = Backbone.View.extend({myid:id ,

        template : _.template(Editcampaigntnpl),
        campaignID : 0,
        initialize : function() {
            _self1=this;
            alert(myid); //this says undefined
            this.campaign = new Campaign();
            this.newscenario = new Newscenarioview();
        },

Solution

  • Just add to your initialize function as first line:

    initialize : function(options) {
            this.campaignID = options.campaignID; // <===
            this.myid = options.campaignID;
    
            _self1=this;
            alert(myid); //this says undefined
            this.campaign = new Campaign();
            this.newscenario = new Newscenarioview();
    },