Tuesday, 15 January 2013

javascript - backbone.js cannot extend router -



javascript - backbone.js cannot extend router -

well, annoying...

i'm using backbone.js require.js. i'm trying define router in separate file , instantiate on page. tried couple of ways , 1 seems work, :

index.phtml

require(['/js/accounting/invoice/index/router.js'], function(router) { window.r = new (backbone.router.extend(router)); });

router.js returns object thats used extend router. syntax seems work me. writing :

window.r = backbone.router.extend(router);

somehow produces variable r empty function.

my question why first approach works , other doesn't ? love fact works , yet i'm frustrated have no thought why. seems wildly different i've seen in tutorials.

that's expected behaviour.

let's check out doing first approach , done sec one.

backbone has special .extend method every class can enhance class prototype, before initialize it.

it looks :

var model = backbone.model.extend({}); var mymodel = new model({ name: 'mymodel' });

what .extend method returns class itself, extended properties have provided. takes .prototype , adds specified argument object. doesn't initialize class. initialize class new classname();.

so if see first approach :

window.r = new (backbone.router.extend(router));

it executes ( equal ) :

var = backbone.router.extend(router); // sec approach ends here window.r = new a();

with sec approach assign window.r non-initalized class object , non-initialized class object normal function.

javascript backbone.js requirejs

No comments:

Post a Comment