1

I am building an app using Meteor and am having trouble understanding the relationship between Routes and Views. I have Routers working properly, but after having done research on calling new Views am baffled.

Do I use App.navigate ? Do I call something like:

var newView = new MyView();

within the proper router function? This is the code I am using (that works) and my app only has two pages - the index page and item view:

var Aphorism = Backbone.Router.extend({
  routes: {
    "saying/:id": "showSaying"
  },
  showSaying: function (id) {
    alert('Saying id ' + id + '.');
  }
});
squeezemylime
  • 2,102
  • 3
  • 18
  • 26

1 Answers1

1

You define what routes exist in the Router. You usually only need one of those, unless you have a very complex app.

Then you hook up links and buttons in the app to execute app.navigate when clicked. You can do this with a view or do it yourself with something like jQuery, it's up to you.

For instance:

<div id="myButton">Click me!</div>

var myView = Backbone.View.extend({
  el: "#myButton",
  events: {
    "click": "go"
  },
  go: function() {
    myRouter.navigate("/someUrl", {trigger: true});
  }
});
Rahul
  • 12,181
  • 5
  • 43
  • 64
  • Thanks Rahul, your example is clear and consie. But how does the app know that the particular view (e.g. myView in your example) is the current / active one, especially in a multi-view app? – squeezemylime Jan 16 '13 at 02:48
  • A "View" is just an object that you can instantiate and use to hook into the DOM. It's up to you to build an architecture around it. A view can be a button, a menu, an entire screen, etc. So for the app to know anything about your views, you'll have to write code that connects the dots. – Rahul Jan 16 '13 at 03:35
  • Ok I see now. Your example works. I guess the use of Views is more open-ended than I previously thought. Thanks! – squeezemylime Jan 16 '13 at 13:28