rodrickmakore
2/18/2013 - 11:57 PM

1-view-events.js

MyView = Backbone.View.extend({
  initialize: function(){
    this.model.bind("change", this.render, this);
  },
  render: function(){ ... },

  onClose: function(){
    this.model.unbind("change", this.render);
  }

});
Backbone.View.prototype.close = function(){
  this.remove();
  this.unbind();
  if (this.onClose){
    this.onClose();
  }
}
Backbone.View.prototype.close = function(){
  this.remove();
  this.unbind();
}
MyRouter = Backbone.Router.extend({
  routes: {
    "": "home",
    "post/:id": "showPost"
  },

  initialize: function(options){
    this.appView = options.appView;
  },

  home: function(){
    var homeView = new HomeView();
    this.appView.showView(homeView);
  },

  showPost: function(id){
    var post = posts.get(id);
    var postView = new PostView({model: post});
    this.appView.showView(postView);
  }
});
function AppView(){

   this.showView(view) {
    if (this.currentView){
      this.currentView.close();
    }

    this.currentView = view;
    this.currentView.render();

    $("#mainContent").html(this.currentView.el);
  }

}
MyRouter = Backbone.Router.extend({
  routes: {
    "": "home",
    "post/:id": "showPost"
  },

  home: function(){
    var homeView = new HomeView();
    $("#mainContent").html(homeView.render().el);
  },

  showPost: function(id){
    var post = posts.get(id);
    var postView = new PostView({model: post});
    $("#mainContent").html(postView.render().el);
  }
});
MyView = Backbone.View.extend({

  initialize: function(){
    this.options.vent.bind("app:event", this.render, this);
  },

  render: function() { ... }
});

var vent = new _.extend({}, Backbone.Events);
new MyView({vent: vent});
vent.trigger("app:event");
MyView = Backbone.View.extend({

  initialize: function(){
    this.model.bind("change", this.render, this);
  },

  render: function(){ ... }
});
MyView = Backbone.View.extend({

  events: {
    "click #someButton": "doThat",
    "change #someInput": "changeIt"
  },

  doThat: function(){ ... },
  changeIt: function(){ ... }
});