0

I'm trying to use require.js for the first time, however I'm getting the error:

Uncaught TypeError: Property '$' of object # is not a function.

 require.config({
      shim: {

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

        underscore: {
          exports: '_'
        },
        backbone: {
          deps: ['underscore', 'jquery'],
          exports: 'Backbone'
        }
      },
      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',
        templates: '/referralgenie/templates/'
      }

    });

    require([
      'app'
    ], function(App){
      App.initialize();
    });

It states the error is in backbone.js so I'm finding it hard to debug.

The error comes from the line here:

Backbone.history.start();

In my Router file:

// Filename: router.js
define([
  'jquery',
  'underscore',
  'backbone',
  'views/CampaginView',
  'views/RewardView',
  'views/FriendRewardView',
  'views/ParticipantView'
], function($, _, Backbone, CampaginView, RewardView, FriendRewardView, ParticipantView) {


   var AppRouter = Backbone.Router.extend({
           routes: {
               ':id': 'portal-home' // matches http://example.com/#anything-here
           }
    });


  var initialize = function(){

    var app_router = new AppRouter;

    app_router.on('route:portal-home',function(id) {
      var campaignView = new CampaginView();
      campaignView.render({id: id});
    });




    Backbone.history.start();
  };
  return {
    initialize: initialize
  };
});

How about just triggering a change ?

$(document).ready(function() {
    $(".accept").on('change', function () {
        if ($(this).val() == "0") {
            $(".generateBtn").addClass("disable");
        } else {
           $(".generateBtn").remove("disable");
        }
    }).trigger('change');
});

I'm guessing you meant to write removeClass and not remove, if so :

$(document).ready(function() {
    $(".accept").on('change', function () {
        $(".generateBtn").toggleClass('disable', this.value=='0');
    }).trigger('change');
});
4

1 回答 1

2

这是基于这个SO question/answer并基于阅读Backbone.js的代码的预感。

我认为 Backbone 无法找到 jQuery,因此您必须自己设置它。尝试通过使用带有initBackbone 功能的 shim 来做到这一点:

    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone',
      init: function (_, $) { Backbone.$ = $; return Backbone; }
    }
于 2013-11-11T00:38:46.897 回答