Ember.js处理View事件后过渡到路由 [英] Ember.js Transition to route after handling View event

查看:62
本文介绍了Ember.js处理View事件后过渡到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

我有一个Ember应用程序,它支持使用Imgur API上传图像.

我已经有一个可以使用的路由和模板来处理任何Imgur ID,但是我想在上传新图像(使用返回的Imgur ID)后过渡到该路由

以下是应用程序的相关部分: http://jsbin.com/iGOXivEW/2/edit

问题:

  1. 这种对View的正确使用和处理变更事件的正确方法是吗?
  2. 如何从视图过渡到"imgur"路线并将新的Imgur ID传递给它?

解决方案

您可以在视图内部处理ui事件.通过移动将图像发布到控制器的代码,您可以将发送到igur或其他地方的逻辑与视图逻辑分开.

可以通过从视图调用来完成转换,

this.controller.transitionToRoute('imgur',imgurId);

或者如果您将所有内容移至控制器,

transitionToRoute('imgur',imgurId);

( http://emberjs.com/api/classes/Ember.Controller.html#method_transitionToRoute )

明智的设计,无论将图像发布的逻辑放在哪里,如果将转换逻辑移动到一个共同的位置(即控制器,甚至更好地移动到路线),并在特定的动作(即已完成)内,您可能会有所帮助.

这样,您现在始终可以在哪里维护从当前路线导航的逻辑.要在控制器中调用动作或从视图或控制器中路由,您需要使用函数 send ,即在控制器 this.controller.send('completed')内部> this.send('completed'); .动作需要放置在动作对象中.

( http://emberjs.com/guides/views/handling-events/)

Setup:

I've got an Ember app that supports image uploading using the Imgur API.

I already have a working Route and Template to handle any Imgur ID, but I want to transition to this route after a new image is uploaded (using the Imgur ID that is returned)

Here is the relevant section of the app: http://jsbin.com/iGOXivEW/2/edit

Questions:

  1. Is this proper use of a View and the correct way to handle the change event?
  2. How can I transition to the 'imgur' route from the View and pass it the new Imgur ID?

解决方案

You handle the ui event inside view which is fine. You could possibly separate the logic of sending to igur or somewhere else, from the view logic, by moving the code of posting the image to the controller.

The transition can be accomplished by calling from view,

this.controller.transitionToRoute('imgur', imgurId);

or if you move everything to the controller,

transitionToRoute('imgur', imgurId);

(http://emberjs.com/api/classes/Ember.Controller.html#method_transitionToRoute)

Design wise, regardless of where the logic of posting the image goes, you may find helpful if you move the transition logic to a common place i.e. the controller or even better to the route, inside a specific action i.e. completed.

This way you always now where to maintain logic that handles navigation from the current route. To call an action in controller or route from view or controller you need to use function send i.e. inside viewthis.controller.send('completed'), inside controller this.send('completed');. The actions need to be placed within an action object.

(http://emberjs.com/guides/views/handling-events/)

这篇关于Ember.js处理View事件后过渡到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆