0

如何使用下面的脚本来扩展 jquery $.fn.serializeObject() 和 require.js?

我收到以下错误:

未捕获的类型错误:无法读取未定义的 serializeObject.js 的属性“fn”:(匿名函数)

   $.fn.serializeObject = function() {
      var o = {};
      var a = this.serializeArray();
      $.each(a, function() {
          if (o[this.name] !== undefined) {
              if (!o[this.name].push) {
                  o[this.name] = [o[this.name]];
              }
              o[this.name].push(this.value || '');
          } else {
              o[this.name] = this.value || '';
          }
      });
      return o;
    };

这是我的视图保存功能给出的错误:

 var participantDetails = $(ev.currentTarget).serializeObject();

它需要我错过的其他依赖吗?

这是我的 main.js:

require.config({

  shim: {

   "jquery": {
        exports: '$'
    },

    underscore: {
      exports: '_'
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone',
      init: function (_, $) { Backbone.$ = $; return Backbone; }
    },
    backbone_tastypie: {
      deps: ['backbone',  'underscore', 'jquery'],
      attach: "Backbone"
    },
     serialize: {
      deps: ['jquery']
      //exports: 'jQuery.fn.serializeObject'
    }
  },
  paths: {
    jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min',
    underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min',
    backbone: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min',
    backbone_tastypie: '/static/js/libs/backbone-tastypie',
    serialize: '/static/js/libs/serializeObject',
    text: 'text',
    templates: '/static/'

  }

});

require([
  'app'
], function(App){

  App.initialize();

});
4

1 回答 1

1

你在定义一个模块吗?你需要:

define([
  'jquery'
],

etc
于 2013-11-12T11:06:09.940 回答