0

我想让加载的页面运行“搜索”功能,但我在 self.search(event) 中收到错误消息。请在那里提供一些帮助,我如何页面加载调用搜索功能?

directory.views.SearchPage = Backbone.View.extend({

templateLoader: directory.utils.templateLoader,

initialize: function() {
    this.template = _.template(this.templateLoader.get('search-page'));
    this.render();
    var keyword = '_firstkey';
    $(document).ready(function (event) {
    self.search(event); //Uncaught TypeError: Object [object global] has no method 'search'##

    })
},

render: function(eventName) {
    $(this.el).html(this.template(this.model.toJSON()));
    this.listView = new directory.views.EmployeeListView({el: $('.scroll', this.el), model: this.model});
    this.listView.render();
    return this;
},

events: {
    "keyup .search-key": "search"
},
 search: function(event) {
    var key = $('.search-key').val();
    this.model.findByName(key);
    return false;
},
});
4

1 回答 1

1

您还没有self在方法中定义变量initialize,您需要创建一个闭包变量,例如var self = this;

directory.views.SearchPage = Backbone.View.extend({

    templateLoader: directory.utils.templateLoader,

    initialize: function() {
        this.template = _.template(this.templateLoader.get('search-page'));
        this.render();
        var keyword = '_firstkey';
        var self = this; //add self variable here
        $(document).ready(function (event) {
            self.search(event); //Uncaught TypeError: Object [object global] has no method 'search'##

        })
    },

    render: function(eventName) {
        $(this.el).html(this.template(this.model.toJSON()));
        this.listView = new directory.views.EmployeeListView({el: $('.scroll', this.el), model: this.model});
        this.listView.render();
        return this;
    },

    events: {
        "keyup .search-key": "search"
    },
    search: function(event) {
        var key = $('.search-key').val();
        this.model.findByName(key);
        return false;
    }
});
于 2013-06-06T03:50:46.303 回答