使用 jQuery UI 对话框与 Backbone.js 和 RequireJS

Using jQuery UI Dialog with Backbone.js and RequireJS

我想在我的第一个使用 backbone 的网页上做弹出窗口。

define(['jquery',
'jqueryui',
'underscore',
'backbone',
'api',
'text!' + versions.getVersionedPath('templates/form.html')

 function ($, jqueryui, _, Backbone, Api, Form) {
    var widok = Backbone.View.extend({
        formularz: _.template(Form),
        el: 'body',
        events: {
            'click #test': 'test',
            'click .del': 'usun',
            'click #elo': 'test2'
        },
        initialize: function () {
            this.$el.html(this.formularz());
            self = this;
            console.log('This model has been initialized.');
            this.render();
        },
        render: function () {
            console.log('Showing up');
                    this.$el.html(this.formularz());
        },
        test: function () {
            self.showNews();
            return false;

        },
        test2: function () {
            $("#dialog-confirm").dialog({
                resizable: false,
                height: 140,
                modal: true,
                buttons: {
                    "Delete all items": function () {
                        $(this).dialog("close");
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }
            });
        }
    });

    return {initialize: function () {
            console.log('Initialize');
            new widok;
            self.showNews();
            console.log('blablablablabla');
        }};

});

我有这样的功能,但是当我尝试使用它时出现了类似

的错误
Uncaught TypeError: $(...).dialog is not a function.

我定义了jquery和jqueryui。有人可以帮助我吗?

根据要求uirejs 配置

shim: {
  jqueryui: {
    "deps": ['jquery']
  },

您分享的代码有语法错误,您没有关闭依赖数组。

jQuery UI 从版本 1.11.0 开始支持 AMD。这是与 AMD 加载程序一起使用的 guide

因此,如果您使用的是最新版本,请从配置中删除 shim

还要注意模块中的 jqueryui 将是 undefined 因为它不导出任何东西,所以最好在依赖项的末尾添加这样的东西,例如:

define([
  'jquery',
  'underscore',
  'backbone',
  'api',
  'text!' + versions.getVersionedPath('templates/form.html'),
  'jqueryui'],
function ($, _, Backbone, Api, Form) {});