Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify routing by removing mvc/Controller #138

Open
zship opened this issue Jul 29, 2014 · 0 comments
Open

Simplify routing by removing mvc/Controller #138

zship opened this issue Jul 29, 2014 · 0 comments
Milestone

Comments

@zship
Copy link
Contributor

zship commented Jul 29, 2014

The path that a route takes from a url change to a View rendering seems a bit complex to me:

(init/popstate event) -> Router#exec -> Route#exec -> Controller#(method) -> Controller#view -> ViewManager#load -> View#render

There's also boilerplate in writing every Controller name and method in both the Controller subclass and app.js.

How about this instead?

// app.js

// was referenced through Controller#viewManager
var viewManager = require('lavaca/mvc/ViewManager');

var app = new Application(function() {
  this.router.add({
    // adaptation of mutualmobile/lavaca-starter app/net/HomeController
    '/': function index(params, state) {
        var model = new Model();
        return viewManager.load(HomeView, model) // instead of Controller#view()
            .then(function() {
                // instead of Controller#history, use HTML5 History API
                History.pushState(state, 'Home Page', params.url);
            });
    },
    '/other': function() {
    }
  });
});

That'd remove mvc/Controller. Controller implementations are usually pretty short (one or two methods), so the above seems doable for all of an application's routes. If a user wanted to split them up, though, they could do something like this:

// app/net/HomeController
define(function(require) {
    var viewManager = require('lavaca/mvc/ViewManager');
    //just a plain object
    return {
        '/': function index(params, state) {
            var model = new Model();
            return viewManager.load(HomeView, model)
                .then(function() {
                    History.pushState(state, 'Home Page', params.url);
                });
        }
    };
});
// app/net/OtherController
define(function(require) {
    var viewManager = require('lavaca/mvc/ViewManager');
    return {
        '/test': function test(params, state) {
            var model = new Model();
            return viewManager.load(TestView, model)
                .then(function() {
                    History.pushState(state, 'Test', params.url);
                });
        }
    };
});

And then in app.js:

// app.js

var mixIn = require('mout/object/mixIn');
var HomeController = require('app/net/HomeController');
var TestController = require('app/net/TestController');

var app = new Application(function() {
  this.router.add(mixIn(
    HomeController,
    TestController
  ));
});
@zship zship added this to the 3.0 milestone Jul 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant