Skip to content

Instantly share code, notes, and snippets.

@DavidBennettPIO
Last active January 4, 2016 21:29
Show Gist options
  • Select an option

  • Save DavidBennettPIO/8681606 to your computer and use it in GitHub Desktop.

Select an option

Save DavidBennettPIO/8681606 to your computer and use it in GitHub Desktop.

Revisions

  1. DavidBennettPIO revised this gist Jan 29, 2014. 2 changed files with 42 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion routeVC.js
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ can.routeVC = can.Construct.extend('can.routeVC', {
    self.constructor.data = data;
    $( attachTo ).html(
    can.view(
    self.constructor.view_dir+config.action+self.constructor.view_ext,
    self.constructor.view_dir+config.view+self.constructor.view_ext,
    self.constructor.data
    )
    );
    41 changes: 41 additions & 0 deletions usage.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    var Todo = can.Model({
    findAll : 'GET /todos',
    findOne : 'GET /todos/{id}',
    create : 'POST /todos',
    update : 'PATCH /todos/{id}',
    destroy : 'DELETE /todos/{id}'
    },{})


    var TodosControl = can.Control.extend({
    init: function() {
    console.log("init Todos");
    },
    index: function( params, render ) {

    can.when(Todo.findAll()).then(function(todos){
    render({todos:todos});
    });
    },
    show: function( params, render ) {

    can.when(Todo.findOne(params['id'])).then(function(todo){
    render({todo:todo});
    });
    }
    });


    new can.routeVC('#view',
    {
    "todos route": {
    control: TodosControl,
    action: 'index'
    },
    "todos/:id route" : {
    control: TodosControl,
    action: 'show'
    }
    });

    can.route.ready()
  2. DavidBennettPIO created this gist Jan 29, 2014.
    73 changes: 73 additions & 0 deletions routeVC.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    can.routeVC = can.Construct.extend('can.routeVC', {
    routerControl:null,
    view_dir: 'views/',
    view_ext: can.view.ext,

    data: {}
    },{

    init: function(attachTo, routes){

    var self = this;

    var route_config = {}

    if(routes.hasOwnProperty('init'))
    {
    route_config['init'] = routes.init;
    delete routes.init;
    }

    var route_render = function(config){

    var render = function(data){
    self.constructor.data = data;
    $( attachTo ).html(
    can.view(
    self.constructor.view_dir+config.action+self.constructor.view_ext,
    self.constructor.data
    )
    );
    }


    return function(params){

    if(config.hasOwnProperty('action'))
    {
    var control = new config.control();
    control[config.action](params, render);
    }else{
    new config.control(params, render);
    }


    }

    }

    can.each(routes, function(config, route){

    if(!config.hasOwnProperty('view'))
    {
    if(config.hasOwnProperty('action'))
    {
    config.view = config.action;
    }else{
    config.view = 'index';
    }
    }


    route_config[route] = route_render(config);
    })

    var RouterControl = can.Control.extend(route_config);

    self.constructor.routerControl = new RouterControl(window);



    }

    });