简体   繁体   中英

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. I have a Ember Object as below

`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.

My main intention is to make a transition once the 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.

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. 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 . When you click the button within my-component.hbs . The my-object instances dummyVariable is toggled and the observer within my-object executes. The tricky part here is that I passed index route itself as ownerRoute property to my-object instance; so that I can trigger the index route's dummyAction from within my-object.js with

ownerRoute.send('dummyAction');

so that related action executes and transition to my-route is performed. 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. 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 . 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.

Anyway, please take a look at the twiddle and ask more if you need to. I will be happy to help if I can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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