简体   繁体   English

如何从Ember控制器访问查询参数

[英]How to access query parameters from an Ember controller

I am working on a habit tracking app . 我正在开发一个习惯跟踪应用程序 I start off with a list of habits. 我从习惯列表开始。 When a user clicks a habit, it creates a new event for that habit. 当用户单击习惯时,它将为该习惯创建一个新事件。 I have the list working, but I am having difficulty with the creation of the new event. 我有工作清单,但是创建新活动有困难。

To create a new event, I have this route: 要创建一个新事件,我有以下路线:

App.Router.map( function() {
  ⋮
  this.resource( 'new_event', { path: '/event/new/:habit_id' } )
} )

Then the associated router is: 然后关联的路由器是:

App.NewEventRoute = Ember.Route.extend( {
  model: function(params) {
    return this.store.find( 'habit', params.habit_id )
  },
  setupController: function( controller, model ) {
    controller.set( 'selectedHabit', model )
  }
} )

To the best of my knowledge, the only place I can access the query params is within the model function. 据我所知,唯一可以访问查询参数的地方是在模型函数中。

The controller, then, is: 那么,控制器为:

App.NewEventController = Ember.ObjectController.extend( {
  init: function() {
    console.log( 'init', this.get( 'selectedHabit' ) )
    // Logic for adding a new event for the habit
    this.transitionToRoute( 'events' )
  },
  selectedHabit: null,
  ⋮

The returned value for selectedHabit is null . selectedHabit的返回值为null

This actually makes sense, since when calling setupController the controller is being passed as a parameter thus has already been initialized (eg http://emberjs.jsbin.com/cuwadafu/1/edit ). 这实际上是有道理的,因为在调用setupController ,控制器已作为参数传递,因此已被初始化(例如http://emberjs.jsbin.com/cuwadafu/1/edit )。 This means that the init function has already been called and the selectedHabit property has not been set yet. 这意味着已经调用了init函数,并且尚未设置selectedHabit属性。

One solution could be to create a generic function for creating the event ( this could also be called from other places as well ) with all the logic, that observes the selectedHabit property. 一种解决方案可能是使用观察到selectedHabit属性的所有逻辑来创建用于创建事件的通用函数( 也可以从其他地方调用该函数)。

App.NewEventController = Ember.ObjectController.extend( {
/*passing an optional parameter of habitId, to make this generic, could be an option*/
  addNewEvent:function(/*habitId*/){
  // Logic for adding a new event for the habit
    this.transitionToRoute( 'events' )
  }.observes("selectedHabit"),
  .
  .
});

This was answered on the Ember discussion board . 在Ember讨论板上对此进行了回答 Instead of using a route, I ended up going with an action: 我没有使用路由,而是执行了一个动作:

<script type="text/x-handlebars" id="habits">
  <div id="habits">
    {{#each}}
      <div class="row" {{action 'createEvent' id}}>
        <div class="col-xs-1 color" {{bind-attr style=style}}></div>
        <div class="col-xs-8">{{name}}</div>
        <div class="col-xs-3" data-role="timer">{{format-time-numeric lastTime}}</div>
      </div>
    {{/each}}
  </div>
</script>

Script 脚本

App.HabitsRoute = Ember.Route.extend( {
  actions: {
    createEvent: function( habitId ) {
      var self = this
      var store = this.get( 'store' )
      store.find( 'habit', habitId ).then( function( habit ) {
        store
          .createRecord( 'event', {
            habit: habit,
            time: new Date()
          } )
          .save().then( function( event ) {
            habit.get( 'events' ).then( function( events ) {
              if( ! events ) {
                events = []
                habit.set( 'events', events )
              }
              events.pushObject( event )
              habit.save().then( function() {
                self.transitionTo( 'events' )
              } )
            } )
          } )
      } )
    }
  }
} )

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

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