我如何在CanJS中实现路由器 [英] How do I Implement router in CanJS

查看:100
本文介绍了我如何在CanJS中实现路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https://github.com/thinkadoo/Projects 应用程序的帮助。我已经借助此应用程序构建了一个类似的应用程序。我的应用程序使用的是d3图表,而不是此图表。我的应用将路由器初始化为

I am taking help of the https://github.com/thinkadoo/Projects application. I have built a similar app with the help of this one. My application is using d3 charts instead of the one this uses. My app initializes the routers as

  var patientStatus = new PatientStatus('#application', {'credentials':Credentials,'secret':Secret});

现在,如果我想实现Router,那么应该做哪些更改?
这是我的JSFiddle和这两种实现。第一个正在工作。但是稍后我在其中初始化路由器的部分似乎不起作用。
http://jsfiddle.net/sweety1112/YMAjm/

Now if i want to implement Router then what changes should be done? Here is my JSFiddle with both implementations. The first one is working. But the later part where in I am initializing the Router doesnt seem to work. http://jsfiddle.net/sweety1112/YMAjm/

有人可以帮我吗。

推荐答案

这里是更新的Fiddle 显示了路由的工作方式:

Here is an updated Fiddle that shows how routing works:

  var Router = can.Control({
    defaults: {}
  }, {
    init: function() {
      // this.element.html(can.view('#index', {}));
    },

    ':type/:id route': function(data) {
        console.log('Type:', data.type);
        console.log('Id:', data.id);
    }
  });

  can.route.ready(false);
  new Router('#content');
  can.route.ready(true);

基本上,您要做的是初始化命名的占位符,并告诉控制器这应该由路线处理器。现在,如果您转到#!test / 23 这样的URL,则处理程序的数据将包含 type id 属性。

Basically, what you do is initialize your named placeholders and tell the controller that this should be handled by the route processor. Now if you go to a URL like #!test/23 the data of the handler will contain a type and id property.

这篇关于我如何在CanJS中实现路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆