backbone.js如何包含自定义函数

时间:2013-07-08 12:44:09

标签: jquery backbone.js

下面是我的html页面/视图页面包含我的问题是如何在我的视图/ js文件中包含我的切换功能

camp.html

 <table cellpadding="0" cellspacing="0" border="0" class="camp" id="camp">

    <tr>
     <th width="5%"><input type="checkbox" onChange="toggle(this)" class="tick_mark" /></th>
     <th width="45%">Campaign Name</th>
    </tr>
  </table>

EDIT 视图/ camp.js

     define([
        'jquery',
        'underscore',
        'backbone',
        'text!templates/camp.html',
        'collections/camp',
        'views/campviews',
        'constants'
        ], function( $, _, Backbone,  allcamp, Campaignscollection, Campaignsview, Constants ) {
           if (!CM.Views) {
              CM.Views= {};
           }
           CM.Views.camp = Backbone.View.extend({

           template: _.template(allcamp),

           initialize: function() {
              this.on("change:name", function(model){
                     var name = model.get("name"); // 'Stewie Griffin'
                     alert("Changed my name to " + name );
                 });
              _self = this;
              this.campaigns = new Campaignscollection();
              this.campaignsview = new Campaignsview({collection:this.campaigns});
              this.campaigns.fetch({
                 success: function () {
                 // _self.render();   
                      $(_self.el).find("#camp").append(_self.campaignsview.render().el);

                 }
              });
           },
   events:{
    'change .tick_mark': 'toggle'
    },
  toggle : function(){
     alert("//");
  }, 

           toggle: function( newChildsName ){
                 //this.set({ child: newChildsName });
              alert("in toggle");
             },
           render: function() {
              $(this.el).html(this.template());
              return this;
           }
        });

        return CM.Views.camp;
     });

1 个答案:

答案 0 :(得分:0)

我认为正确的方式(不引人注目)会在输入中放入一个id或一个类:

<th width="5%"><input type="checkbox" id="myInput" /></th>

然后在您的视图中将事件侦听器附加为:

events:{
        'change #myInput': 'toggle'
        }