Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone not working Node (Headless TDD / Development)

I'm trying to use Vows (BDD) to test MyApp, using Backbone (which needs JQuery). I want to use NodeJS to get headless development, but I'm getting the error A), when I try to execute the code in B). Basically, the JQuery object is not in the scope of the Backbone library. But everything was require'd / imported as in C). Does anyone successfully have Backbone working on NodeJS. I'm aware of the nodechat (downloaded the code and tried to run it) and backbone models on server blog post, examples, but they are not working with what I've set up. I'm using Node(v0.5.10-pre) and Backbone v(0.5.3).

A)


    TypeError: Cannot call method 'ajax' of undefined
    at [object Object]. (/js/backbone.js:1082:14) 
    ... 

B)


    var fubar = new myapp.models.Fubar();   // this class already has a url set 
    fubar.fetch();

C)


    var requirejs = require('js/r.js'); 
    requirejs.config({ nodeRequire: require, baseUrl: 'js', }); 

    var vows = require('vows');
    var assert = require('assert');

    //** Hack to get a Window object for JQuery
    var zombie = require("zombie");
    var browser = new zombie.Browser;
    browser.window.location = "http://localhost:3000";
    window = browser.window;

    //** Hack due to Sinon not being designed to work in NodeJS; putting some stubs to get it working
    sinon = require('sinon');
    var xhr = require("sinon/lib/sinon/util/fake_xml_http_request");
    var fakeServer = require("sinon/lib/sinon/util/fake_server").fakeServer;

    requirejs([ 'jquery-1.6.4', 'json2', 'underscore', 'backbone', '_**myapp**_', ]); 

I'm assuming underscore is pulled in and used correctly. But I don't think I've gotten far enough to confirm.

Thanks

like image 762
Nutritioustim Avatar asked Aug 10 '26 11:08

Nutritioustim


1 Answers

Ok, I was able to get this working by having node do the require:


    jQuery = $ = require("jquery");
    requirejs([ 'json2', 'underscore', 'backbone', "storydesk", ]);

Now, I need to plug in a Sinon fakeServer and see how

like image 197
Nutritioustim Avatar answered Aug 13 '26 06:08

Nutritioustim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!