Question

What is the difference between use as this.initialize(); within router.js in BackboneJS and

require(['router'],function(Router) {
    router      = Router;
    router.intialize();                         
});

How to get same instances?

Can any one help me, Thanks in advance

EDIT:

I want to implement code coverage Unit Testing (Jasmine.js 1.3.1).

For this, I need to pass router (exactly same instance what I have when running application without code coverage tool) instance to my Jasmine spec's .js file.

If it is not possible, to get same instance during specs execution, Kindly advise me How to get the instance using different approach.

If you need any clarification in this transition, please let me know.

Thanks in advance.

Was it helpful?

Solution

The difference is the constructor. Backbone classes all have a method which gets run when you instantiate the class. That constructor method is how your initialize method gets called when you do new Backbone.Router().

As you can see in, the constructor for Router:

var Router = Backbone.Router = function(options) {
    options || (options = {});
    if (options.routes) this.routes = options.routes;
    this._bindRoutes();
    this.initialize.apply(this, arguments);
};

the main difference between calling initialize and instantiating a router is that when you instantiate it the routes are bound.

Well, that's the main Backbone difference. There's also the Javascript difference between calling new A.B() and A.B(), which is that the new keyword creates a new object and sets it as the this inside your constructor/initialize function, whereas calling it directly the way you're doing sets this to the Router class itself.

Because you almost certainly don't want to be using your Router class as the this when you test your methods, what you likely want to do is just instantiate a Rotuer properly in your setup code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top