Meteor JS Meteor页面分页.set()重新运行控制器

时间:2015-06-01 10:58:36

标签: javascript meteor meteor-autoform

过去几个小时我试图解决我的问题,问题是meteor pages package和它的.set()函数导致我的控制器代码每次更改时重新运行(即使是从javascript控制台)。让我向您展示我的代码片段:

autoform hook file:

AutoForm.hooks
  handsFilters:
    onSubmit: (data) ->
      # AutoForm.debug()
      # debugger
      @event.preventDefault()

      extendFiltersForHoldem data

      RecentHandsPagination.set
        filters: new RecentHandsFilters(data).setFilters()
      $('#dropdown-recent-hands.open').removeClass('open')
      console.log "onSubmit hook"
      @done()

html中的autoform表单:

{{#autoForm schema='HandsFilterSchema' id='handsFilters' resetOnSuccess=false doc=handsFiltersDefaultDoc}}
              <div class="filters-header">
                <h4>{{_ 'filters'}}</h4>
                <a id="clear-form"> {{_ 'clear'}} </a>
              </div>
              <div class="filters-main">
                <div class="row">
                  <div class="col-lg-4 filters-game">
                    <label>{{_ 'features.hands.labels.games'}}</label>
                    {{> afQuickField name='pokerGames' id='pokerGames'}}
                  </div>
                  <div class="col-lg-4 filters-type">
                    <label>{{_ 'features.hands.labels.types'}}</label>
                    {{> afQuickField name='pokerTypes'}}
                  </div>
                  {{> filtersVariants}}
                </div>
              </div>
              {{> filtersChoose}}
              <div class="filters-footer">
                <button data-action='close-hands-filter' type='submit'>{{_ 'buttons.apply'}}</button>
                <small>{{_ 'globalWords.or'}}</small>
                <a href="">{{_ 'buttons.cancel'}}</a>
              </div>
            {{/autoForm}}

控制器文件:

class @RecentHandsController extends RouteController
  layoutTemplate: 'recentHands'

  waitOn: ->
    usersIds = _.uniq(Hands.find().map (hand) -> hand.userId)
    Meteor.subscribe 'usersWithImages', usersIds

  data: ->
    Hands.find().fetch()

  onBeforeAction: ->
    console.log "doing onBeforeAction"
    RecentHandsPagination.set
      filters: new RecentHandsFilters().setFilters()
    @next()

我的分页实例:

@RecentHandsPagination = new Meteor.Pagination Hands,
  availableSettings:
    filters: true
    sort: true
  templateName: 'hands'
  itemTemplate: 'handItem'
  perPage: 30
  infinite: true
  infiniteRateLimit: 1
  infiniteTrigger: 3
  dataMargin: 0
  maxSubscription: 1000
  divWrapper: true

现在,当我在表单上单击“应用”(用于设置流星页包的过滤器)时,我可以在浏览器的js控制台中看到这一点:

onSubmit hook
recent_hands_controller.coffee:12 doing onBeforeAction
recent_hands_filters.coffee:11 <<<<<<<<<<<@RecentHandsFilters>>>>>>>>>>>>>
recent_hands_controller.coffee:12 doing onBeforeAction
recent_hands_filters.coffee:11 <<<<<<<<<<<@RecentHandsFilters>>>>>>>>>>>>>

1 个答案:

答案 0 :(得分:0)

问题是你的按钮:

<button data-action='close-hands-filter' type='submit'>{{_ 'buttons.apply'}}</button>

将代码更改为:

<button data-action='close-hands-filter' id="applybutton">{{_ 'buttons.apply'}}</button>

在模板中给它一个事件,以防止事件冒泡提交它:

Template.yourTemplateName.events({
    'click #applybutton':function(e,tmpl) {
        e.preventDefault();
    }
});