rodrickmakore
2/18/2013 - 11:57 PM

gistfile1.js

Backbone.View.prototyp.start = function() {
    if (this.model && this.modelEvents) {
        _.each(this.modelEvents,
        function(functionName, event) {
            this.model.bind(event, this[functionName], this)
        },
        this)
    }
	return this;

}

Backbone.View.prototyp.stop = function() {
    if (this.model && this.modelEvents) {
        _.each(this.modelEvents,
        function(functionName, event) {
            this.model.unbind(event, this[functionName])
        },
        this)
    }
}

MyView = Backbone.View.extend({
    modelEvents: {
        'change': 'render'
    }
});

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

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

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

    showPost: function(id) {
        this.cleanActualView();
        var post = posts.get(id);
        var postView = new PostView({
            model: post
        }.start();
        this.appView.showView(postView);
		this.actualView = postView;
    },
    cleanActualView: function() {
        this.actualView.stop();
    }

});