which does in fact function.bind, which is just another way to do the this/that dance. only it is slower.
Am Dienstag, 13. August 2013 21:30:15 UTC+2 schrieb John Goodsen: > > This is one of the main reaons I prefer to write my code in Coffeescript. > You don't have to do the this/that dance thing. You just use the => > notation to define functions. > > On Tuesday, August 13, 2013 1:34:57 AM UTC-7, bodo wrote: >> >> Thank you guy >> >> On Tuesday, August 13, 2013 2:46:55 AM UTC+2, Rick Waldron wrote: >>> >>> `this.collection` doesn't yet exist, so the `else` path in `saveData` is >>> being taken, which is sent to `connectDB` which calls >>> `MongoClient.connect`, but does not bind `this` to the callback. >>> >>> This change should work: >>> >>> this.connectDB(this.saveData.bind(this)); >>> >>> >>> >>> >>> >>> On Mon, Aug 12, 2013 at 7:22 PM, bodo <[email protected]> wrote: >>> >>>> I did a pretty code: >>>> >>>> var Square = function(id,order,color,state) { >>>> >>>> this.id = id; >>>> this.order = order; >>>> this.color = color; >>>> this.state = state; >>>> this.connected = false; >>>> >>>> MongoClient.connect(config.mongoURL, (function(err, db) { >>>> this.connected = true; >>>> this.db = db; //db >>>> this.collection = db.collection("squares"); //collection >>>> }).bind(this)); >>>> } >>>> >>>> Square.prototype.connectDB = function(callback){ >>>> >>>> (function(that){ >>>> MongoClient.connect(config.mongoURL, function(err, db) { >>>> console.log("connected"); >>>> this.connected = true; >>>> this.db = db; //db >>>> this.collection = db.collection("squares"); //collection >>>> >>>> //callback(that); >>>> callback(); >>>> >>>> }); >>>> >>>> })(this); >>>> } >>>> >>>> >>>> Square.prototype.test = function(){ >>>> console.log("test"); >>>> } >>>> >>>> //export data >>>> Square.prototype.exportData = function(){ >>>> >>>> var rtArr = { >>>> id:this.id, >>>> order:this.order, >>>> color:this.color, >>>> state:this.state >>>> }; >>>> >>>> return rtArr; >>>> } >>>> >>>> //save datas to mongo >>>> Square.prototype.saveData = function () { >>>> >>>> if (this.collection){ >>>> >>>> *var doc = this.exportData();* >>>> this.collection.insert(doc,function() { >>>> console.log('inserted'); >>>> }); >>>> }else{ >>>> this.connectDB(this.saveData); >>>> } >>>> } >>>> >>>> var square = new Square(6,1,'red',1); >>>> square.saveData(); >>>> >>>> When I run this code, that display: >>>> >>>> throw err >>>> ^ >>>> TypeError: Object #<Object> has no method 'exportData' >>>> >>>> Why it can not understand *var doc = this.exportData(); ?* >>>> * >>>> * >>>> Someone can help me ? Thank you very much >>>> >>>> -- >>>> -- >>>> Job Board: http://jobs.nodejs.org/ >>>> Posting guidelines: >>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >>>> You received this message because you are subscribed to the Google >>>> Groups "nodejs" group. >>>> To post to this group, send email to [email protected] >>>> To unsubscribe from this group, send email to >>>> [email protected] >>>> For more options, visit this group at >>>> http://groups.google.com/group/nodejs?hl=en?hl=en >>>> >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "nodejs" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> For more options, visit https://groups.google.com/groups/opt_out. >>>> >>>> >>>> >>> >>> -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
