1

Plz help me, I'm using the following code:

index.js:

var $ =  require('./node_modules/jquery/dist/jquery.min');
var _ = require('./node_modules/underscore/underscore-min');
var Backbone = require('./node_modules/backbone/backbone-min');
require('./model/models');

run();

function run(){
    var session = new ****.***.Models.CustomerSession();
    session.save({
        "authenticators": ["********","****"]
    }).always(function(xhr){
        do_something...
    });
}

models.js:

**** = {};
****.*** = {};
****.***.Models = {};

var _ = require('../node_modules/underscore/underscore-min');
var Backbone = require('../node_modules/backbone/backbone-min');

/////////////MODELS/////////////

****.***.Models.CustomerSession = Backbone.Model.extend({
    urlRoot : 'https://*****.****.com/api/***/customer_session/'
});

****.***.Models.Transaction = Backbone.Model.extend({
    urlRoot : 'https://*****.****.com/api/***/transaction/'
});

****.***.Models.FaultType = Backbone.Model.extend({
    urlRoot : 'https://*****.****.com/api/***/fault_type/'
});

****.***.Models.FaultTypeSet = Backbone.Model.extend({
    urlRoot : 'https://*****.****.com/api/***/fault_type/'
});

****.***.Models.Task = Backbone.Model.extend({
    urlRoot : 'https://*****.****.com/api/***/task/'
});
///////////////////////////////

And get the following ERROR: "Cannot read property 'ajax' of undefined"... This is all the code in the project except the node_modules libraries... I think that backbone don't recognize the $ sign, but I'm not sure and don't know what to do with the problem... Plz help me...

Here is the error log as I see it:

c:\ce\test_script\node_modules\backbone\backbone-min.js:1
h:"PATCH","delete":"DELETE",read:"GET"};e.ajax=function(){return e.$.ajax.appl
                                                                ^
TypeError: Cannot read property 'ajax' of undefined
at Object.e.ajax (c:\ce\test_script\node_modules\backbone\backbone-min.js:1:14801)
at e.sync (c:\ce\test_script\node_modules\backbone\backbone-min.js:1:14518)
at i.extend.sync (c:\ce\test_script\node_modules\backbone\backbone-min.js:1:3208)
at i.extend.save (c:\ce\test_script\node_modules\backbone\backbone-min.js:1:5716)
at run (c:\ce\test_script\index.js:14:13)
at Object.<anonymous> (c:\ce\test_script\index.js:10:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)

Process finished with exit code 8

Here is the error log when using backbone instead of backbone-min:

c:\ce\test_script\node_modules\backbone\backbone.js:1209
return Backbone.$.ajax.apply(Backbone.$, arguments);
                 ^
TypeError: Cannot read property 'ajax' of undefined
at Object.Backbone.ajax (c:\ce\test_script\node_modules\backbone\backbone.js:1209:22)
at Backbone.sync (c:\ce\test_script\node_modules\backbone\backbone.js:1188:38)
at _.extend.sync (c:\ce\test_script\node_modules\backbone\backbone.js:286:28)
at _.extend.save (c:\ce\test_script\node_modules\backbone\backbone.js:493:18)
at run (c:\ce\test_script\index.js:17:13)
at Object.<anonymous> (c:\ce\test_script\index.js:10:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)

Process finished with exit code 8
Vladi Isakov
  • 289
  • 6
  • 16
  • In order for us to help you, we need more information on the error. Does it say in which file the error occured, on which line, etc. Give us as much information as possible – CerebralFart Jan 11 '15 at 14:46
  • The jQuery library does not seem to be loaded. May be you need to `require` it in models.js too. – meskobalazs Jan 11 '15 at 14:47
  • add `var $ = require('./node_modules/jquery/dist/jquery.min');` to the second file and you be golden – neo Jan 11 '15 at 16:58
  • Thanks guys but it still not working – Vladi Isakov Jan 11 '15 at 18:41
  • You may need to pass in jquery or define where jquery is for require especially if this is node side. There is some scoping there such that var $ may not be on global. – Ted Johnson Jan 11 '15 at 18:50
  • Can you explain yourself? – Vladi Isakov Jan 12 '15 at 13:19
  • for debug purposes don't use the minified version of backbone, please replace the backbone.min.js with regular source code and then post your report – neo Jan 12 '15 at 14:48
  • I posted the error log with backbone instead of backbone-min... – Vladi Isakov Jan 12 '15 at 14:57
  • I think that the problem is that requier('jquery') not working, I figured it when I tried to do this : console.log($); and got this: [Function]... but I don't know how to solve this problem... – Vladi Isakov Jan 12 '15 at 15:00
  • did you ever resolve this? – Robert Levy Mar 11 '15 at 01:37
  • @RobertLevy You should do: `Backbone.$ = $` to define jQuery dependency. However, ajax won't work in node.js, as it doesn't have the `XMLHttpRequest` object. You'd need a polyfill for that (like [najax](https://github.com/najaxjs/najax)). – George Marques Mar 11 '15 at 05:11

1 Answers1

2

This was buried in the comments above, but I was having the same problem and setting

Backbone.$ = $

fixed the issue for me

Jake
  • 251
  • 4
  • 4