backbone.js将param从路由器传递给视图

时间:2013-07-11 09:40:50

标签: backbone.js backbone-views backbone-events backbone-routing

下面是我的路由器功能,其中我有一个id作为参数。我的问题是如何将id传递到营地视图..谢谢..我尝试了以下..当我在视图中做警报时它说未定义

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();
        },

1 个答案:

答案 0 :(得分:1)

只需将初始化函数添加为第一行:

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();
},