不在反应范围之外调用反应函数

时间:2017-01-16 12:52:23

标签: javascript jquery reactjs react-rails

我有一个反应类如下:

var AllMeals = React.createClass({

  ....

  concatMeal: function(new_meal) {
   newState = this.state.meals.concat(new_meal);
   this.setState({ meals: newState });
  },

  ....

});

module.exports = AllMeals

components.js看起来像这样

require('./constants')

....

window.AllMeals = require('./components/all_meals');

....

application.js看起来像这样

//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require react
//= require react_ujs
//= require react_router
//= require react_router_ujs
//= require cable
//= require components
//= require_tree .

但是,当我在concatMeal文件中的window.AllMeals之外调用meal.coffee函数时,我会得到window.AllMeals.concatMeal is not a function

App.meal = App.cable.subscriptions.create "MealChannel",
  received: (data) ->
    unless data.raw_html_for_meals_index.blank?
      window.AllMeals.concatMeal(data.raw_html_for_meals_index)

我做错了什么?

0 个答案:

没有答案