简体   繁体   English

如何在Ember.js中重复一个资源

[英]How can I repeat a resource in Ember.js

I have a page resource that uses the page title in the url. 我有一个页面资源,使用网址中的页面标题。

App.Router.map(function () {
    this.resource('page', {
        path: '/:page_id'
    });
});

App.PageRoute = Ember.Route.extend({
  serialize: function(model) {
    return { page_id: model.title};
  }
});

That is working fine in this jsbin . 这在jsbin中工作得很好。 However, I would like to have subpages nested in the url like this: 但是,我想将子页面嵌套在url中,如下所示:

localhost/#/main_page/sub_page 本地主机/#/ main_page / SUB_PAGE

I tried to make a sub resource ( jsbin ), but I'm not sure if it is the right approach. 我试图创建一个子资源( jsbin ),但我不确定它是否是正确的方法。

App.Router.map(function () {
    this.resource('page', {path: '/:page_id'},
                  this.resource('subpage', {path: '/:page_id/:subpage_id'}));
});

There are two main problems in my attempt: I have to repeat my page view and it doesn't retain the parent page in the url. 我的尝试有两个主要问题:我必须重复我的页面视图,并且不会在网址中保留父页面。 I'm getting: 我越来越:

localhost/#/undefined/sub_page 本地主机/#/未定义/ SUB_PAGE

Am I heading in the right direction? 我正朝着正确的方向前进吗? Can this be accomplished with just one resource? 这可以用一个资源完成吗?

Thanks in advance! 提前致谢!

Have you considered nesting resources? 你考虑过嵌套资源吗?

App.Router.map(function () {
    this.resource('page', {path: '/:page_id'}, function(){
        this.resource('subpage', {path: '/:subpage_id'});
    });
});

This would enable at least the URL structure you asked for, but i am not really sure about your requirements. 这将至少启用您要求的URL结构,但我不确定您的要求。

Most of the modifications I've made are in your html and template. 我所做的大多数修改都在你的html和模板中。 Please, don't mash up the Handlebars' link-to helper with classic anchors () and don't change the link-to tagname attribute, or atleast think twice before doing so. 请不要将Handlebars的链接帮助器与经典锚点()混合在一起,并且不要更改链接到标记名属性,或者在执行此操作之前至少要三思而后行。 First - move the testData to a globally accessible object, so that you can use it in the menu: 首先 - 将testData移动到全局可访问的对象,以便您可以在菜单中使用它:

Javascript: 使用Javascript:

App.CustomRoutes = [{
    id: 1,
    title: 'single'
}, {
    id: 2,
    title: 'subpages',
    pages: [{
        id: 3,
        title: 'subpage1'
    }, {
        id: 4,
        title: 'subpage2'
    }, {
        id: 5,
        title: 'subpage3'
    }]
}];

Html: HTML:

<ul class="navbar-nav nav">
  {{#each page in App.CustomRoutes}}
    <li class='menu-item'>
      {{#link-to 'page' page.title}}
        {{page.title}}
        {{#if page.pages}}
          <b class="caret"></b>
        {{/if}}
      {{/link-to}}
      <span class='subpages'>
        <ul>
          {{#each subpage in page.pages}}
            <li>
              {{#link-to 'page.subpage' page.title subpage.title}}
                {{subpage.title}}
              {{/link-to}}
            </li>
          {{/each}}
        </ul>
      </span>
    </li>
  {{/each}}
</ul>

Then I fixed your router declaration. 然后我修复了你的路由器声明。 When you define nested routes, the third parameter to this.resource() is a function, much alike the function you pass to App.Router.map(). 定义嵌套路由时,this.resource()的第三个参数是一个函数,与传递给App.Router.map()的函数非常相似。

App.Router.map(function () {
    this.resource('page', {path: '/:page_id'},
                  function() { this.route('subpage', {path: '/:subpage_id'});});
});

Finally, here are the definitions of your routes. 最后,这里是您的路线的定义。 Because Subpage is nested in Page, the route must be called PageSubpageRoute. 由于Subpage嵌套在Page中,因此必须将路由称为PageSubpageRoute。 If it was nested in Foo, it would have been FooSubpageRoute. 如果它嵌套在Foo中,它将是FooSubpageRoute。 NOTE: The this.render() call inside a router, has the following parameters: (, ). 注意:路由器内的this.render()调用具有以下参数:(,)。

App.PageSubpageRoute = Ember.Route.extend({
  model: function(params) {
    var parentModel = this.modelFor('page');
    var res = $.grep(parentModel.pages, function (e) {
          if (e.title == params.subpage_id) {
            return e;
          }
        });
    return res[0];
  },
  serialize: function(model) {
    return { subpage_id: model.title};
  },
  renderTemplate: function() {
    this.render('subpage');
  }
});

App.PageRoute = Ember.Route.extend({
  serialize: function(model) {
    return { page_id: model.title};
  },
    model: function (params) {
        return $.grep(App.CustomRoutes, function (e) {
          if (e.title == params.page_id) {
            return e;
          }
        })[0];
    }
});

Here is the code: jsbin 这是代码: jsbin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM