简体   繁体   English

Emberjs嵌套的路由和资源

[英]Emberjs nested route and resource

This is a question that I'm not sure how to answer it, I have a nested route like this : 这是一个我不确定如何回答的问题,我有这样的嵌套路由:

Router.map(function() {
    this.resource('pages', function(){
        this.resource('page', { path: '/:id' }, function(){
            this.route('edit', function(){
                this.resource('images', function(){
                    this.resource('image', { path: '/:id'}, function(){
                        this.route('edit');
                    })
                }); 
            });
        });
    });
});

But the thing is that I don't know if my edit route ( this.route('edit', function(){}) ) should be a route or a resource . 但问题是我不知道我的编辑路径( this.route('edit', function(){}) )应该是route还是resource

Because in my app, I will navigate like this : (let's take the id 1 for pages and 2 for images) 因为在我的应用中,我将这样导航:(让我们以id为1表示页面,以id 2表示图像)

/pages/ => /pages/1/ => pages/1/edit => /pages/1/edit/images => /pages/1/edit/images/2 /pages/ => /pages/1/ => pages/1/edit => /pages/1/edit/images => /pages/1/edit/images/2

But one problem is that when I setup the {{link-to}} like this : 但是一个问题是,当我像这样设置{{link-to}}

page = this.modelFor('page'); 页面= this.modelFor('page');

{{#each item in model}}
    {{#link-to "image" page item}}
        {{list-row item=item}} //component to display row in a table
    {{/link-to}}
{{else}}
    {{no-items}} 
{{/each}}

My list of links looks like that : 我的链接列表如下所示:

/pages/1/edit/images/1
/pages/2/edit/images/2
/pages/3/edit/images/3

It's like the page id is using the same id as the image which I don't understand how that is possible :/ 就像页面ID与图片使用相同的ID,我不知道怎么可能这样:/

[edit] : [编辑]:

When I click on an image inside /images/ it doing two transition and I have this in my console as debug : 当我单击/images/内的/images/它进行了两次转换,并且在控制台中将其作为debug:

      Rendering image with default view <webmob@view:default::ember844> Object {fullName: "view:image"}
vendor.js:28547 Could not find "image.index" template or view. Nothing will be rendered Object {fullName: "template:image.index"}
vendor.js:28547 Transitioned into 'pages.page.edit.images.image.index'
vendor.js:28547 Could not find "images.index" template or view. Nothing will be rendered Object {fullName: "template:images.index"}
vendor.js:28547 Transitioned into 'pages.page.edit.images.index'

[edit2]: [EDIT2]:

After I've created folder for my pages (I had the same warning for those route), it gives me this : 在为页面创建文件夹之后(对于那些路由我有相同的警告),它给了我:

Could not find "pages" template or view. Nothing will be rendered Object {fullName: "template:pages"}
vendor.js:28547 Rendering pages.index with default view <webmob@view:default::ember518> Object {fullName: "view:pages.index"}
vendor.js:28547 Transitioned into 'pages.index'

[edit3]: This is the console output when my app structure is like this : [edit3]:这是我的应用程序结构如下时的控制台输出:

- templates/
-    pages.hbs
-    page.hbs
-    //other resources


 generated -> route:pages.index Object {fullName: "route:pages.index"}
webmob/routes/pages.js:12 DEBUG: Already log in
VM69786:161 Ember Inspector Active
vendor.js:28547 Rendering application with default view <webmob@view:toplevel::ember480> Object {fullName: "view:application"}
vendor.js:28547 Rendering pages with default view <webmob@view:default::ember489> Object {fullName: "view:pages"}
vendor.js:28547 generated -> controller:pages.index Object {fullName: "controller:pages.index"}
vendor.js:28547 Could not find "pages.index" template or view. Nothing will be rendered Object {fullName: "template:pages.index"}
vendor.js:28547 Transitioned into 'pages.index'
vendor.js:28547 generated -> route:page.index Object {fullName: "route:page.index"}

I have everything display fine, but always having the warning. 我一切正常,但始终收到警告。 When I create a pages folder like this : 当我创建这样的页面文件夹时:

- templates/
    -    pages/
        - index.hbs //empty file
    -    pages.hbs
    -    page.hbs
    -    //other resources

I have this in my console : 我在控制台中有这个:

generated -> route:pages.index Object {fullName: "route:pages.index"}
webmob/routes/pages.js:12 DEBUG: Already log in
vendor.js:28547 Rendering application with default view <webmob@view:toplevel::ember419> Object {fullName: "view:application"}
vendor.js:28547 Rendering pages with default view <webmob@view:default::ember428> Object {fullName: "view:pages"}
vendor.js:28547 generated -> controller:pages.index Object {fullName: "controller:pages.index"}
vendor.js:28547 Rendering pages.index with default view <webmob@view:default::ember443> Object {fullName: "view:pages.index"}
vendor.js:28547 Transitioned into 'pages.index'
vendor.js:28547 generated -> route:page.index Object {fullName: "route:page.index"}
VM70411:161 Ember Inspector Active

In one of my projects, I stumbled upon a similar issue. 在我的一个项目中,我偶然发现了一个类似的问题。 I just checked my source code: 我刚刚检查了我的源代码:

// Routing map
App.Router.map( function() {
    this.resource('projects', function() {
        this.resource('project', {path: ':name'}, function() {
            this.resource('edit', function() {
                this.resource('type', {path: '/type/:type_name'});
            });
        });
    });
});

I can access and edit a type inside a project with: /projects/MyNewProject/edit/type/RandomType (since I use my types' names and not their ids). 我可以使用/projects/MyNewProject/edit/type/RandomType访问和编辑项目中的/projects/MyNewProject/edit/type/RandomType (因为我使用类型的名称而不是其ID)。

I remember noticing the exact same behavior as you did: if I used :name in both project and type resources, the generated link would be something like /projects/MyNewProject/type/MyNewProject . 我记得曾注意到与您完全相同的行为:如果我在projecttype资源中都使用:name ,则生成的链接将类似于/projects/MyNewProject/type/MyNewProject So having the same :id parameter for both your page and image resources probably is the cause of the problem. 因此,为pageimage资源使用相同的:id参数可能是导致此问题的原因。 You might try :image_id for your image resource, and stick with :id for your page resource. 您可以尝试:image_id作为image资源,并坚持:id作为page资源。

As for the other aspect of your question, regarding whether you should use route or resource for your first edit route: the Ember Routing guide suggests that "You should use this.resource for URLs that represent a noun, and this.route for URLs that represent adjectives or verbs modifying those nouns." 至于您的问题的其他方面,关于您是应该为第一个edit路径使用route还是resource :《 灰烬路由》指南建议“对于代表名词的URL,您应该使用this.resource;对于那些表示URL的URL,请使用this.route。代表修饰这些名词的形容词或动词。”

But it doesn't seem so straightforward in your example, since you have a "resource" route nested inside an "action" route. 但这在您的示例中似乎并不那么简单,因为您有一个嵌套在“操作”路由内的“资源”路由。 I think you should use a resource , since the endpoint of your route (an image) is a resource. 我认为您应该使用resource ,因为路线(图像)的终点是资源。

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

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