1

I started following Organizing your application using Modules (require.js tutorial, but after adding first event handler to my view - I encountered problem:


// Filename: views/project/list
define([
  'jquery',
  'underscore',
  'backbone',
  'handlebars',
  'collections/projects',
  'text!templates/projects/list.js'
], function ($, _, Backbone, Handlebars, ProjectsCollection, projectListTemplate) {
    var ProjectListView = Backbone.View.extend({
        el: $('#container'),
        events: {
            "click .open-proj": "openProject",
        },
        initialize: function () {
          ...
        },
        render: function () {
            ...
        },
        openProject: function(e) {
            // HERE I WANT TO TRIGGER ROUTING VIA router.navigate
            alert("opened");
        }
    });
    // Our module now returns our view
    return ProjectListView;
});

In the openProject callback I want to trigger routing, but I cannot introduce dependency to app.js as it would cause circular dependency(router depends on view). How should I handle this?

1 Answer 1

3

You can pass your router to the ProjectListView when you create it:

var projectListView = new ProjectListView({
    router: app_router
});

Since ProjectListView is a Backbone.View, it can then access the router using this.options.router, no circular dependency problem here.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes it might work, but I used define routing callbacks inside extend() not in initialize()
I'm not sure I understand, you can access this.options.router from within openProject
I couldn't get routes to trigger when binding inside initialize(), see: stackoverflow.com/questions/13582974/… So I cannot set the reouter when I create the view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.