2012-11-05 67 views
-1

火正在試圖綁定在我的Backbone.js的兩個功能查看,但不知道爲什麼它不工作..Backbone.js的結合2個功能一起按順序

任何一個可以建議?

define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'libs/jquery_ui_dependency', 
    'vehicle/js/collections/MakeSet', 
    'vehicle/js/collections/ModelSet', 
    'vehicle/js/collections/TrimSet', 
    'vehicle/js/collections/YearSet', 
    'vehicle/js/views/Paginator' 
], function($, _, Backbone, JQueryUi, MakeSet, ModelSet, TrimSet, YearSet, Paginator){ 
    'use strict'; 
    var BrowseVehicleView = Backbone.View.extend({ 
     el: $('#vehicle-browse-form'), 
     initialize: function(){ 

      JQueryUi.init(); 

      _.bindAll(this, 'render', 'onMakeChange'); 

      //define array of elements to be used in DOM manipulations 
      this.elements = { 
       "make"  : $('#id_make',  this.el), 
       "model"  : $('#id_model',  this.el), 
       "trim"  : $('#id_trim',  this.el), 
       "year_from" : $('#id_year_from', this.el), 
       "year_to" : $('#id_year_to', this.el), 
       "price_from" : $('#id_price_from', this.el), 
       "price_to" : $('#id_price_to', this.el) 
      } 




     }, 
     events: { 
      "change #id_make"   : "onMakeChange", 
      "change #id_model"   : "onModelChange", 
      "change #id_trim"   : "onTrimChange" 
     }, 
     render: function(){ 

      Paginator.filter({ 
       model__make: this.elements.make.val(), 
       model__model: this.elements.model.val(), 
       model__trim: this.elements.trim.val() 
      });  

     }, 
     onMakeChange: function(event) { 

      this.resetElement([ 
       this.elements.model, 
       this.elements.trim, 
       this.elements.year_from, 
       this.elements.year_to 
      ]); 

      // load models 
      this.RenderCollection(ModelSet, {make: this.elements.make.val()}, [this.elements.model]); 


     }, 

什麼想在這裏實現的,是每次onMakeChange調用它應該調用渲染功能以及渲染Paginator.filter()

+1

'resetElement'做了什麼? 「RenderCollection」呢?通常你只需要將'render'綁定到一個事件(通常是''change''或''reset''),然後做一些事情來觸發這個事件。要麼或者手動調用'render'。 –

回答

1

_.bindAll確保當方法被調用,'this'的值被設置爲您指定的值。因此,_.bindAll(this,'render','onMakeChange')指定當render或onMakeChange被調用時,BrowseVehicleView被設置爲'this'的值。

與mu說的一樣,您可能想要在onMakeChange()內部手動調用this.render(),或將this.render作爲事件處理程序綁定到您的模型引發的其中一個事件。

+0

謝謝你的隊友解釋它。我完全對bindAll的目的感到困惑:) –