How to correctly add a jQuery UI autocomplete widget using Backbone.js

前端 未结 4 2161
花落未央
花落未央 2021-02-08 00:53

I am in the process of learning Backbone.js. I currently assume that if one is using Backbone.js then all client-side javascript/jQuery should be integrated with Backbone. From

4条回答
  •  长情又很酷
    2021-02-08 01:17

    Attache all your plugins when you render the view:

    you can do something like this:

    render: function () {
    
      var view = this;
      // Fetch the template, render it to the View element and call done.
    
      application_namespace.fetchTemplate(this.template, function (tmpl) {
        var viewModel = view.model.toJSON();
        view.$el.html(tmpl(viewModel));
    
        view.$("#categories").autocomplete({
          minLength: 1,
          source: function (request, response) {
            $.getJSON("url" + view.storeId, {
                term: request.term,
              }, function (data) {
                response($.map(data, function (item) {
                  return {
                    value: item.title,
                    obj: item
                  };
              }));
            });
          },
    
          select: function (event, ui) {
            //your select code here
            var x = ui.item.obj;
            var categories = view.model.get("x");
    
            // bla bla
          }
          error: function (event, ui) {
            //your error code here
          }
        }
      });
    }
    

    Hope that helps

提交回复
热议问题