Javascript 相对链路和主干.js路由器

Javascript 相对链路和主干.js路由器,javascript,jquery,html,backbone.js,Javascript,Jquery,Html,Backbone.js,我在使用backbone.js中的router函数时遇到了这个问题。这可能是一件小事,但我似乎无法理解这一点,也无法在谷歌上找到任何东西 问题:http://www.mydomain.com/user/1有一个链接。此链接应链接到http://www.mydomain.com/user/1/profile 当然,如果我使用我会得到我想要的,但是1是一个动态生成的值。那么我的路由器应该如何定义路由呢?我认为将号码1硬编码到路线中不是明智的选择 //Router var AppRouter = B

我在使用backbone.js中的router函数时遇到了这个问题。这可能是一件小事,但我似乎无法理解这一点,也无法在谷歌上找到任何东西

问题:
http://www.mydomain.com/user/1
有一个链接。此链接应链接到
http://www.mydomain.com/user/1/profile

当然,如果我使用
我会得到我想要的,但是
1
是一个动态生成的值。那么我的路由器应该如何定义路由呢?我认为将号码
1
硬编码到路线中不是明智的选择

//Router

var AppRouter = Backbone.Router.extend({

    routes: {
        '': 'profile',
        'profile': 'profile'
    },

    profile: function() {

    }
});

var app = new AppRouter();
Backbone.history.start();
当我像
那样设置
标签的
href
属性时,得到的链接是
http://www.mydomain.com/user/profile

对于
我得到
http://www.mydomain.com/user/profile

对于
我得到
http://www.mydomain.com/profile

对于
我得到
http://www.mydomain.com/profile/


为什么缺少
1
,我如何保持它以实现我想要的?

如中所示,您可以在路由中使用参数,如中所示。
'/user/:id/profile'

如中所示,您可以在路由中使用参数,正如在
'/user/:id/profile'

中一样,您不能直接在HTML中定义此动态URL,必须在视图中创建它们

例如:

模板:

<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="<%= url_profile %>">profile</a>
</script>
// code simplified and no tested
var Element = Backbone.Model.extend({
  initialize: function(){
    this.set( "url_profile", this.url() + "/profile" );
  }
});

var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    return this;
  }
});
<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="#replace_with_real_url">profile</a>
</script>
// no Element.url_profile attribute needed:
var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    this.$el.find( ".profile" ).attr( "href", "/user/" + this.model.id + "/profile" );
    return this;
  }
});
或者,就像我有时遇到阻碍时所做的那样:

模板:

<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="<%= url_profile %>">profile</a>
</script>
// code simplified and no tested
var Element = Backbone.Model.extend({
  initialize: function(){
    this.set( "url_profile", this.url() + "/profile" );
  }
});

var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    return this;
  }
});
<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="#replace_with_real_url">profile</a>
</script>
// no Element.url_profile attribute needed:
var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    this.$el.find( ".profile" ).attr( "href", "/user/" + this.model.id + "/profile" );
    return this;
  }
});

您不能直接在HTML中定义这个动态URL,您必须在视图中创建它们

例如:

模板:

<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="<%= url_profile %>">profile</a>
</script>
// code simplified and no tested
var Element = Backbone.Model.extend({
  initialize: function(){
    this.set( "url_profile", this.url() + "/profile" );
  }
});

var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    return this;
  }
});
<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="#replace_with_real_url">profile</a>
</script>
// no Element.url_profile attribute needed:
var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    this.$el.find( ".profile" ).attr( "href", "/user/" + this.model.id + "/profile" );
    return this;
  }
});
或者,就像我有时遇到阻碍时所做的那样:

模板:

<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="<%= url_profile %>">profile</a>
</script>
// code simplified and no tested
var Element = Backbone.Model.extend({
  initialize: function(){
    this.set( "url_profile", this.url() + "/profile" );
  }
});

var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    return this;
  }
});
<script type="text/template" id="template-element">
  <h1><%= title %></h1>
  <a class="profile" href="#replace_with_real_url">profile</a>
</script>
// no Element.url_profile attribute needed:
var ElementView = Backbone.View.extend({
  template: _.template( $("#template-element").html() ),

  render: function(){
    this.$el.html( this.template( this.model.toJSON() ) );
    this.$el.find( ".profile" ).attr( "href", "/user/" + this.model.id + "/profile" );
    return this;
  }
});

谢谢,如何通过包含相对路径获得指向
/user/1/profile
的链接?
:id
在生成的链接中不断消失,因此我最终使用了
/user/profile
谢谢,如何通过包含相对路径获得指向
/user/1/profile
的链接?
:id
在生成的链接中不断消失,因此我最终与
/user/profile