简体   繁体   English

Ember.js路线图

[英]Ember.js Route Mappings

I am very new to ember.js and I am try to setup the routes/resources for my application in App.Router.map , however I am having a bit of trouble with coming up with the most efficient/DRY-est way to define the routes that the app needs. 我是ember.js的新手,我尝试在App.Router.map为我的应用程序设置路由/资源,但是我在App.Router.map最有效/最干的方法来定义时遇到了一些麻烦应用所需的路线。

I want to have a resource items , which is to be the default route when coming into the application, it displays a list of items which is filtered by a dynamic segment :bucket , that must be one of set of predefined bucket names. 我想拥有一个资源items ,这是进入应用程序时的默认路由,它显示由动态段:bucket过滤的项列表,该段必须是一组预定义的桶名称。 ie #/items/foo , or #/items/bar , where foo and bar are valid :bucket values. #/items/foo#/items/bar ,其中foobar是有效的:bucket值。

Additionally, the items route should also allow a second segment called tag , which then must be followed by another dynamic segment which is a url-safe version of the tag name, ie #/items/tag/my-tag . 此外, items路由还应允许第二个段称为tag ,然后必须跟随另一个动态段,该动态段是标记名称的url安全版本,即#/items/tag/my-tag

I have the first part working, using: 我有第一部分工作,使用:

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

However, i am unable to figure out how to fit the tag version of the route in there. 但是,我无法弄清楚如何在其中安装路线的tag版本。 I've tried both nesting it inside the items resource and also as its own top level resource, but neither works. 我尝试过将其嵌套在items资源中,也作为其自己的顶级资源进行嵌套,但是都没有用。

You can structure your router like this: 您可以这样构造路由器:

App.Router.map(function() {
  this.resource('items', {path: '/items'}, function(){
    this.route('bucket', {path: '/:bucket'});
    this.route('tag', {path: '/tag/:tag'});
  });
});

This shows all Item s at #/items and filters by bucket at #/items/bucketNameHere and by tags at #/items/tag/tagNameHere . 这将显示#/items所有Item ,并按#/items/bucketNameHere的存储桶和#/items/tag/tagNameHere

If all items are displayed in the ItemRoute : 如果所有项目都显示在ItemRoute

App.ItemsRoute = Ember.Route.extend({
  model: function(){
      return this.store.find('item');
  }
});

Then you can handle the filtering in the ItemsBucketRoute and ItemsTagRoute : 然后,您可以在ItemsBucketRouteItemsTagRoute处理过滤:

App.ItemsBucketRoute = Ember.Route.extend({
  model: function(params){
    console.log("model fired");
    return this.modelFor('items').filterProperty('bucket', params.bucket);
  }
});

App.ItemsTagRoute = Ember.Route.extend({
  model: function(params){
    return this.modelFor('items').filterProperty('tag', params.tag);
  }
});

You can also accomplish list filtering using the filterProperty of arrayController s . 您还可以使用filterPropertyarrayController完成列表过滤。

JSBin Example JSBin示例

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

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