Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the Backbone.js router to navigate through views modularized with require.js
    primarykey
    data
    text
    <p>I am separating my views and router into separate files with require. I then have a main.js file that instantiates the router, and also renders my default view.</p> <p>My router has view ('View/:id') and edit ('Edit/:id') as routes. In main.js, when I instantiate the router, I can hardcode router.navigate('View/1', true) and the navigation works fine. In my view file, when I click on the edit link, I want to call router.navigate('View/' + id, true), but I'm not sure how I should do this.</p> <p>I've had success calling Backbone.history.navigate('View/' + id, true), but I don't feel like I should be relying on the global Backbone object.</p> <p>I tried passing ({ router: appRouter }) to my views so I could use this.options.router.navigate(), however that wasn't working for me.</p> <p>In case you're curious, here's a bunch of code from my app:</p> <p>Router:</p> <pre><code>define(['./View', './Edit'], function (View, Edit) { return Backbone.Router.extend({ routes: { 'View/:id': 'view', 'Edit/:id': 'edit' }, view: function (id) { var model = this.collection.get(id); var view = new View({ model: model }); view.render(); }, edit: function (id) { var model = this.collection.get(id); var edit = new Edit({ model: model }); edit.render(); } }); }); </code></pre> <p>View:</p> <pre><code>define(function () { return Backbone.View.extend({ template: Handlebars.compile($('#template').html()), events: { 'click .edit': 'edit' }, render: function () { //Create and insert the cover letter view $(this.el).html(this.template(this.model.toJSON())); $('#View').html(this.el); return this; }, edit: function () { Backbone.history.navigate('Edit/' + this.model.id, true); }, }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload