D
D
deadkEEper12015-10-18 23:59:19
HTML
deadkEEper1, 2015-10-18 23:59:19

Why is the view not rendered when navigating to the url via router.navigate?

And so, there is a router on 2url

var Router = Backbone.Router.extend({
  routes: {
    ''		:   'homepage',
    'users' :  'user'
  },

  users : function(){
    var  userView = new UsersView();
       userView.render();

  },

  homepage: function(){
    var homepageView = new HomepageView();
      homepageView.render();
  }


})

The start page has text and one button.
view constructor
var HomepageView = Backbone.View.extend({
  el: 'body',
  events: {
    'click  #button': 'show'
  },

  show: function(){
    router.navigate('users', {trigger: true})
  },
  template: _.template($("#homepageTemplate").html()),
  render: function(){
  this.$el.html(this.template())
  return this;	
  }

})

And here is the template.
<script type="text/template" id="homepageTemplate">
  <h1>Welcome</h1>
     <button id="button">Button</button>
</script>

When the button is clicked, the router on 'users' is triggered (it can be seen in the browser line, localhost:3030/#users) , but nothing is rendered. And if you manually drive in the url 'users', then everything is ok.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Latyshev, 2015-10-19
@magalex

Nothing is rendered because you are following a link inside the page without contacting the server (see what an anchor is )

D
Dmitry Kravchenko, 2015-10-19
@mydearfriend

router.navigate('users', {trigger: true,replace:true})

router.navigate doc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question