Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 定义参数时,主干视图事件似乎不执行函数_Javascript_Backbone.js_Coffeescript_Backbone Events - Fatal编程技术网

Javascript 定义参数时,主干视图事件似乎不执行函数

Javascript 定义参数时,主干视图事件似乎不执行函数,javascript,backbone.js,coffeescript,backbone-events,Javascript,Backbone.js,Coffeescript,Backbone Events,我的一个主干视图有以下代码: class GD.Views.Result extends Backbone.View template: JST['mobile/templates/result'] tagName: 'tr' className: 'result' events: 'click' : 'showDetail' 'click #favourite' : 'addFavourite(Favourite)' 'click #like' : 'a

我的一个主干视图有以下代码:

class GD.Views.Result extends Backbone.View
  template: JST['mobile/templates/result']
  tagName: 'tr'
  className: 'result'
  events:
    'click' : 'showDetail'
    'click #favourite' : 'addFavourite(Favourite)'
    'click #like' : 'addFavourite(Like)'
    'click #dislike' : 'addFavourite(Dislike)'
   render: ->
     $(@el).html(@template(model: @model))
     this
   addFavourite: (type) ->
     event.preventDefault()
     attributes =
       id: @model.id
       type: type
       cluster: @model.cluster
     @collection.create attributes,
     wait: true
     success: @updateIcons
     error: @handleError
   showDetail: ->
     ...
   updateIcons: ->
     ...
   handleError: ->
     ...
我在我的控制台中发现了这个错误:

Uncaught Error: Method "addFavourite(Favourite)" does not exist
我真的不明白为什么AddFavorite方法而不是showDetail方法会发生这种情况-您不允许将需要定义参数的方法传递给任何事件吗


非常感谢您的帮助(!)

不要在事件配置中指定参数。事件配置用作查找,它是通过名称而不是通过方法签名完成的

class GD.Views.Result extends Backbone.View
  template: JST['mobile/templates/result']
  tagName: 'tr'
  className: 'result'
  events:
    'click' : 'showDetail'
    'click #favourite' : 'addFavourite'
    'click #like' : 'addFavourite'
    'click #dislike' : 'addFavourite'
   render: ->
     $(@el).html(@template(model: @model))
     this
   addFavourite: (type) ->
     event.preventDefault()
     attributes =
       id: @model.id
       type: type
       cluster: @model.cluster
     @collection.create attributes,
     wait: true
     success: @updateIcons
     error: @handleError
   showDetail: ->
     ...
   updateIcons: ->
     ...
   handleError: ->
     ...

不要在事件配置中指定参数。事件配置用作查找,它是通过名称而不是通过方法签名完成的

class GD.Views.Result extends Backbone.View
  template: JST['mobile/templates/result']
  tagName: 'tr'
  className: 'result'
  events:
    'click' : 'showDetail'
    'click #favourite' : 'addFavourite'
    'click #like' : 'addFavourite'
    'click #dislike' : 'addFavourite'
   render: ->
     $(@el).html(@template(model: @model))
     this
   addFavourite: (type) ->
     event.preventDefault()
     attributes =
       id: @model.id
       type: type
       cluster: @model.cluster
     @collection.create attributes,
     wait: true
     success: @updateIcons
     error: @handleError
   showDetail: ->
     ...
   updateIcons: ->
     ...
   handleError: ->
     ...

正如@Derick所解释的,如果
events
元素的值是一个字符串,它将命名视图对象上的一个方法,因此不能以这种方式指定参数。您需要(示例在JS中):

  • 仅提供方法名称作为字符串,然后确定方法中的类型。例如

    events : {
    
      'click #favourite' : 'addFavourite',
    
      'click #like' : 'addFavourite',
    
      'click #dislike' : 'addFavourite'
    
    }
    

  • 提供函数对象(在本例中,通过函数表达式)。在这种情况下,您可以提供一个调用泛型
    addFavorite
    方法并将类型传递给它的函数,例如:

    events : {
    
      'click #favourite' : function ( event ) {
    
        this.addFavourite( event, 'favourite' );
    
      },
    
    
      'click #like' : function ( event ) {
    
        this.addFavourite( event, 'like' );
    
      },
    
    
      'click #dislike' : function ( event ) {
    
        this.addFavourite( event, 'dislike' );
    
      }
    
    }
    

  • 从你的例子中可以看出,我选择选项1

    请注意,可以使其更具动态性,例如:

    events : function () {
    
      var events = {};
    
      var view = this;
    
      [
    
        'favourite',
    
        'like',
    
        'dislike'
    
      ].forEach( function ( type ) {
    
        events[ "click #" + type ] = ( function ( type ) {
    
          return function ( event ) {
    
            view.addFavourite( event, type );
    
          };
    
        } )( type );
    
      };
    
    
      return events;
    
    }    
    

    正如@Derick所解释的,如果
    events
    元素的值是一个字符串,它将命名视图对象上的一个方法,因此不能以这种方式指定参数。您需要(示例在JS中):

  • 仅提供方法名称作为字符串,然后确定方法中的类型。例如

    events : {
    
      'click #favourite' : 'addFavourite',
    
      'click #like' : 'addFavourite',
    
      'click #dislike' : 'addFavourite'
    
    }
    

  • 提供函数对象(在本例中,通过函数表达式)。在这种情况下,您可以提供一个调用泛型
    addFavorite
    方法并将类型传递给它的函数,例如:

    events : {
    
      'click #favourite' : function ( event ) {
    
        this.addFavourite( event, 'favourite' );
    
      },
    
    
      'click #like' : function ( event ) {
    
        this.addFavourite( event, 'like' );
    
      },
    
    
      'click #dislike' : function ( event ) {
    
        this.addFavourite( event, 'dislike' );
    
      }
    
    }
    

  • 从你的例子中可以看出,我选择选项1

    请注意,可以使其更具动态性,例如:

    events : function () {
    
      var events = {};
    
      var view = this;
    
      [
    
        'favourite',
    
        'like',
    
        'dislike'
    
      ].forEach( function ( type ) {
    
        events[ "click #" + type ] = ( function ( type ) {
    
          return function ( event ) {
    
            view.addFavourite( event, type );
    
          };
    
        } )( type );
    
      };
    
    
      return events;
    
    }