简体   繁体   English

Ember JS转换到嵌套路由,其中​​所有路由都是视图中的动态段

[英]Ember JS transition to nested routes where all routes are dynamic segments from a view

We are writing an application using EmberJS. 我们正在使用EmberJS编写应用程序。 However we are still new with this framework and we're having a hard time resolving some of what may seem to be straight forward. 然而,我们仍然是这个框架的新手,我们很难解决一些看似直截了当的事情。

The model is pretty simple, there are 3 models: Queue, Task, and Image. 模型非常简单,有3种模型:Queue,Task和Image。 We are using dynamic URI segments for all routes and routes for these models are nested in the form: :queue_id/:task_id/:image_id. 我们对所有路由使用动态URI段,这些模型的路由嵌套在以下形式:: queue_id /:task_id /:image_id。

The routes are configured this way: 路由配置方式如下:

App.Router.map(function() {
   this.resource('queue', {path: ':queue_id'}, function() {
      this.resource('task', {path: ':task_id'}, function() {
         this.resource('image', {path: ':image_id'});
      });
   });
}

And somewhere in the HTML, we have this simple template to iterate through all images from a task: 在HTML的某个地方,我们有这个简单的模板来迭代任务中的所有图像:

{{#each task.images}}
   <li>
      {{#view App.ThumbnailView.contentBinding="this"}}
         <img {{bindAttr src="thumbnail.url"}} />
      {{/view}}
   </li>
{{/each}}

And here is the code for the Thumbnail View: 以下是缩略图视图的代码:

App.ThumbnailView = Ember.View.extend({
   tagName : 'a',
   click : function(e) {
       var task = //assume this value exists;
       var queue = //assume this value exists;
       var image = //assume this value exists;

       this.get('controller.target.router').transitionTo('image', queue, task, image);
   }
});

Finally, here's is our ImageRoute: 最后,这是我们的ImageRoute:

App.Image = Ember.Object.extend();
App.Image.reopenClass({
    find : function(image_id) {
       //This is where I set a breakpoint
       console.log(image_id);
    }
});

App.ImageRoute = Ember.Route.extend({
    model : function(params) {
      //image_id is the last uri segment in: #/1/1/1
      return App.Image.find(params.image_id);
    }
});

The Problem: The call to this.get('controller.target.router').transitionTo() seems to be working. 问题:调用this.get('controller.target.router').transitionTo()似乎正在运行。 I can see that when I click in one of the thumbnails view, the URL changes (eg from /1/1/2 to something like /1/1/3). 我可以看到,当我点击其中一个缩略图视图时,URL会发生变化(例如从/ 1/1/2更改为/ 1/1/3)。 However, I'm not seeing any state change in the UI. 但是,我没有看到UI中的状态发生任何变化。 Also, the line where I put a breakpoint doesn't seem to be triggered. 此外,我放置断点的行似乎没有被触发。 But when I refresh the page, it works well. 但是当我刷新页面时,它运行良好。

Is there something wrong with my transition code? 我的转换代码有问题吗?

Thanks. 谢谢。

Two things to note: 有两点需要注意:

First, instead of 首先,而不是

this.get('controller.target.router').transitionTo('image', queue, task, image);

Use: 使用:

this.get('controller').transitionToRoute('image.index', queue, task, image);

This might not change the behaviour, but it's more Ember idiomatic. 这可能不会改变行为,但它更像是Ember惯用语。

The second thing is the following: 第二件事如下:

Internal transitions will not trigger the model hook on the route, because Ember assumes you are passing the model along with the transition, so no need to call the model hook since you already passed the model. 内部转换不会触发路径上的model挂钩,因为Ember假设您正在传递模型以及转换,因此您无需调用model挂钩,因为您已经通过了模型。

That is why your breakpoint doesn't get triggered, the find isn't being called (as it shouldn't). 这就是为什么你的断点没有被触发,没有调用find (因为它不应该)。

I don't have enough information to find your issue but if I were to guess from the fact that a page refresh works and an internal transition doesn't is that there is an inconsistency between the objects passed to transitionTo and between what is returned from the model hook. 我没有足够的信息来查找您的问题,但是如果我猜测页面刷新是否有效,而内部转换不是因为传递给transitionTo的对象与返回的对象之间存在不一致model钩子。

You should pass the exact object to transitionTo as the ones that would have been returned by the model hook. 您应该将确切的对象传递给transitionTo作为model钩子返回的对象。

If you are doing: 如果你这样做:

this.get('controller').transitionToRoute('image.index', queue, task, image);

and it's not working, something is probably wrong with either queue , task , or image models you are passing. 并且它不起作用,您传递的queuetaskimage模型可能有问题。

So this: 所以这:

   var task = //assume this value exists;
   var queue = //assume this value exists;
   var image = //assume this value exists;

is not very helpful because it could be where the problem is. 不是很有帮助,因为它可能是问题所在。

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

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