从EmberJS的路线观察服务上的房产 [英] Observing a property on a service from a route in EmberJS

查看:54
本文介绍了从EmberJS的路线观察服务上的房产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我不是在理解这个概念。据我所知,任何 Ember.object 都可以观察另一个 Ember.object 的属性。

I think I'm not understanding a concept here. As far as I know any Ember.object can observe properties on another Ember.object.

所以,我有一个服务,一个路由器和一个组件。我需要组件和路由器才能观察服务上的属性。完全有可能我只是以错误的方式构建解决方案,我将概述我最后要做的事情。

So, I have a service, a router, and a component. I need the component and the router to be able to observe a property on the service. It's entirely possible that I'm just structuring the solution in the wrong way, I'll include an overview of what I'm trying to do at the end.

这里差不多就是我所拥有的:

Here is approximately what I have:

export default Ember.Service.extend({
  observedProperty: 'original value'
});



/components/thing-shower.js



/components/thing-shower.js

export default Ember.Component.extend({
  thingManager: Ember.inject.service(),
  myObserver: Ember.observer(
    'thingManager.observedProperty',
    function() {
      // This shows up as anticipated, unlike the one in the routes
      console.log('THING SHOWER COMPONENT observed change on thingManager')
    }
  ),
 actions: {
   changeObservedProperty: function() {
     let thingManager = this.get('thingManager')
     let newText = thingManager.get('observedProperty') + '!'
     // here i am sure to call `set` to make sure observers fire
     thingManager.set('observedProperty', newText)
   }
 }

});



/routes/things.js



/routes/things.js

export default Ember.Route.extend({
  thingManager: Ember.inject.service(),

  underObservation: Ember.observer('thingManager.observedProperty', function() {
    // This is what I expect to fire, but does not.
    console.log('THINGS ROUTE observed change on thingManager')
  }),
});

正如你所看到的,我期待组件和路由器中两个观察者的控制台输出。为什么这不起作用?

As you can see, I'm expecting console output from both observers in the component and router. Why doesn't this work?

在这里旋转!

这可能是一个单独的问题,但我想知道是否有更好的方法来完成我想要做的事情。我一直在学习'数据下降,行动起来',这让我采用了这种方法。我正在建立一个网站,用一堆GPS坐标加载一个json文件并将它们粘贴在地图上。

This is perhaps a separate question, but I'd like to know if there is a better way to accomplish what I'm trying to do. I've been learning about 'data down, actions up', which led me to this approach. I'm building a website that load a json file with a bunch of GPS coordinates and sticks them on a map.

目标是单击地图标记,然后加载相应的数据。这也应该改变路线。因此,我的想法是,在服务中跟踪我的标记,并且当所选标记发生变化时,路由器将观察到并转换到下一个路径。该组件还会注意到已更改的属性并更新地图。

The goal is to click a map marker, and have that load the corresponding data. This should also change the route. So, my thinking was, to keep track of my markers in a service, and when the selected marker changes, the router would observe that and transition to the next route. The component would also notice the changed property and update the map.

谢谢大家!

推荐答案

things.js 您未使用的路径文件访问/使用 thing-manager 服务,因此不会触发观察者。

In things.js route file you haven't used accessed/used thing-manager service, so observer will not be triggered.

routes / thing.js

routes/thing.js

init(){
    this._super(...arguments);
    this.get('thingManager');
},

介绍这将使你的观察者被解雇。

introducing this will make your observer to be fired.

我想说,如果您关注DDAU原则,那么您的组件不应该改变事物管理器服务属性。它应该发送行动服务并改变它。

I would say, if you are following the DDAU priniciple, then your component should not mutate the thing-manager service properties. it should send action to service and mutate it.

注意:你可以在任何Ember.Object中拥有观察者和计算属性,这意味着你也可以拥有它的事物经理服务。

Note: You can have observers and computed properties inside any Ember.Object which means you have it thing-manager service too.

这篇关于从EmberJS的路线观察服务上的房产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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