javascript - How to pass a route parameter to the main View? -
i use simple router route page requests. start app instantiating bvapp.
recently, added in routing , use first parameter page1
. how can pass parameter bvapp code.
i know should not pass in via constructor parameters allowed backbone uses _.pick
. see here
can call method on bvapp perhaps?
i looked @ so post ideas after googled search pulled nothing obvious.
perhaps entry point application should not backbone view @ all?
// brmain var brmain = backbone.router.extend({ name: 'brmain', routes: { "*page1(/:supertag)(/:tag)": "main" } }); var router = new brmain(); // matches // domain.com // domain.com#page1 // domain.com#page1/supertag // domain.com#page1/supertag/tag router.on('route:main', function (page1, supertag, tag) { var app = $a.mod.add(new bvapp()); }); backbone.history.start(); // bvapp var bvapp = backbone.view.extend({ name: 'bvapp', el: window, initialize: function () { var public_page = $a.reg.get('pub_req'); var token = storage.gettoken();
the initialize
function takes optional options
object, same object pass view's constructor.
router.on('route:main', function(page1, supertag, tag) { // pass arguments inside object here var app = $a.mod.add(new bvapp({ page: page1 })); }); var bvapp = backbone.view.extend({ name: 'bvapp', el: window, initialize: function(options) { options = options || {}; // can use them here this.page = options.page; } });
this not explained inside backbone's documentation, common pattern of objects. example, here's sample of backbone's view source.
var view = backbone.view = function(options) { this.cid = _.uniqueid('view'); _.extend(this, _.pick(options, viewoptions)); this._ensureelement(); // lastly, call initialize passing arguments along this.initialize.apply(this, arguments); };
in fact, arguments passed constructor in backbone passed along initialize
function.
Comments
Post a Comment