backbone.js绑定两个函数一起按顺序触发

时间:2012-11-05 12:47:20

标签: backbone.js

我试图绑定我的backbone.js视图中的两个函数,但不知道它为什么不起作用..

任何人都可以提供建议吗?

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时它都应该调用render函数来渲染Paginator.filter()

1 个答案:

答案 0 :(得分:1)

_.bindAll确保在调用方法时,将“this”的值设置为您指定的值。因此,_. bindAll(this,'render','onMakeChange')指定当调用render或onMakeChange时,BrowseVehicleView被设置为'this'的值。

就像mu所说,你可能想在onMakeChange()中手动调用this.render(),或者将this.render作为事件处理程序绑定到模型引发的事件之一。