0

So this is my ajax call (pretty much standard jQuery but using the couchdb jquery library, http://daleharvey.github.com/jquery.couch.js/ ):

var stuff = "some stuff";
$.couch.db("test_db").create({
    success: enyo.bind(this, function (data) {
        console.log(stuff);
    })
});
stuff = "a change in stuff";

And I'd like the output of console.log to be "some stuff" rather then "a change in stuff".

The more methods to do this the better, cause I think some methods might need me to not use "enyo.bind" ( http://enyojs.com/ ), but perhaps I can accomplish the same thing a little differently with those methods.

01AutoMonkey
  • 2,515
  • 4
  • 29
  • 47

3 Answers3

1

You can use a function for it.

var create = function (stuff) {
  $.couch.db("test_db").create({
    success: enyo.bind(this, function (data) {
      console.log(stuff);
    })
  });
};

create(stuff);

Or, its equivalent as an anonymous function.

!function(stuff) {
  $.couch.db("test_db").create({
    success: enyo.bind(this, function (data) {
      console.log(stuff);
    })
  });
}(stuff);

See What does the exclamation mark do before the function?

Community
  • 1
  • 1
Alexander
  • 23,432
  • 11
  • 63
  • 73
0

You can "anchor" a variable by doing this:

(function(varname) {
    // code that relies on varname
})(varname);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0
var stuff = "some stuff";
var callback = (function(stuff){ 
    return function(data){ console.log( stuff ) }; 
}(stuff));

$.couch.db("test_db").create({
    success: enyo.bind(this,  callback );
});
stuff = "a change in stuff";
pawel
  • 35,827
  • 7
  • 56
  • 53