[英]Transition to route's action from Ember object
I am fairly new to ember. 我是炭烬新手。 I have an existing Ember App and i need to implement a functionality in it. 我有一个现有的Ember App,我需要在其中实现功能。 I have a Ember Object as below 我有一个Ember对象,如下所示
`import Ember from 'ember'`
CallService = Ember.Object.extend
... other code here
_updateConnectedLeadId: (->
console.log "Do i get here??"
**pass the route action here**
).observes('some_other_here')
`export default CallService`
Unfortunately, i couldn't put the whole code here. 不幸的是,我无法将整个代码放在这里。
My route looks like 我的路线看起来像
ApplicationRoute = Ember.Route.extend
actions:
showLead: ->
console.log data
console.log "did i get here?"
@transitionTo('dashboard')
`export default ApplicationRoute`
I tried using @send('showLead')
, @sendAction('showLead')
in my method but no luck. 我尝试在我的方法中使用@send('showLead')
和@sendAction('showLead')
,但是没有运气。
My main intention is to make a transition once the console.log "Do i get here??"
我的主要目的是在console.log "Do i get here??"
进行过渡。 is displayed. 被陈列。 I am not sure if i am on the right way here. 我不确定在这里我走的路是否正确。
I also tried using @transitionTo('dashboard')
and @transitionToRote('dashboard')
directly but it throws me errors. 我也尝试过直接使用@transitionTo('dashboard')
和@transitionToRote('dashboard')
,但这会引发错误。
I have been stuck for a day on this now and i am clueless. 我已经为此停留了一天,我一无所知。
I'll be grateful for any guidance and help. 如有任何指导和帮助,我将不胜感激。 Thanks 谢谢
You have the problem that you are trying to trigger a route action or trigger a transition from within an Ember.Object
, named call service. 您有一个问题,您试图从名为call service的Ember.Object
内触发路由操作或触发转换。 The code you create is unclear about where your custom object is being created; 您创建的代码不清楚自定义对象的创建位置。 where the observer is triggered due to a change to object's property update, and so on. 由于更改了对象的属性更新而触发了观察者的位置,依此类推。
Nevertheless, I tried to provide a working example for you. 不过,我试图为您提供一个可行的例子 。 If you open the twiddle, you will see that I created a my-object
instance within index.js
route and pass it as model
to my-component
. 如果打开旋转按钮,您将看到我在index.js
路由中创建了一个my-object
实例,并将其作为model
传递给my-component
。 When you click the button within my-component.hbs
. 单击my-component.hbs
的按钮时。 The my-object
instances dummyVariable
is toggled and the observer within my-object
executes. 将切换my-object
实例dummyVariable
并执行my-object
内的观察者。 The tricky part here is that I passed index
route itself as ownerRoute
property to my-object
instance; 这里最棘手的部分是我将index
路由本身作为ownerRoute
属性传递给了my-object
实例; so that I can trigger the index
route's dummyAction
from within my-object.js
with 这样我就可以从my-object.js
内触发index
路由的dummyAction
ownerRoute.send('dummyAction');
so that related action executes and transition to my-route
is performed. 以便执行相关操作并执行到my-route
转换。 Although, I believe this might solve your question; 虽然,我相信这可能会解决您的问题; I am not quite happy about the design. 我对设计不太满意。 I do not think, it is a good way for Ember.Objects to know about routes, actions, controllers, etc. I believe the proper way is observing object's relevant properties from within this constructs and perform necessary actions by their own. 我认为这不是Ember.Objects了解路由,动作,控制器等的好方法。我相信正确的方法是从此构造中观察对象的相关属性,并自行执行必要的动作。 Moreover, you might consider creating a service
instead of an object and inject the service directly to your route instead of creating an instance of your class extending Ember.Object
. 此外,您可以考虑创建service
而不是对象,并将服务直接注入到路由中,而不是创建扩展Ember.Object
的类的实例。 If you have to extend from Ember.Object
you can just inject relevant route, controller, etc to the instances of that particular object class by using an instance-initializer if you need. 如果必须从Ember.Object
扩展, Ember.Object
可以根据需要通过使用实例初始化器将相关的路由,控制器等注入该特定对象类的实例 。
Anyway, please take a look at the twiddle and ask more if you need to. 无论如何,请看看旋转的感觉,并询问更多是否需要。 I will be happy to help if I can. 如果可以的话,我很乐意提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.